Factory Method Pattern With Kotlin in android


I am trying to become a better Android Developer. I am at a stage where I am studying the fragments API. But, my approach to learning Android is a little bit different. I see videos and tutorials and courses: all have been made with Java. I have fallen in love with kotlin.When I follow those courses, I look for ways to do the same things with Kotlin. Here is a common development pattern recommended by the google API documentation when dealing with fragments: The Factory Method Pattern. And here I will explore how to implement that pattern in Kotlin.

Factory Method Pattern

When we make fragments, we tend to bundle arguments with them. This can be done in two ways:

  1. Make the bundle in the activity class, and pass the arguments to the fragment from the activity class that contains the fragment
    1. Bundle up the data you want in a bundle object
    2. Pass that in as an argument from the parent activity class
    3. In the onCreate() method or later in the fragment class, extract the values by calling the getArguments() method
  2. Generate a Factory Method called newInstance() within the fragment to use the Factory Method Pattern
    1. Create a newInstance() method within the fragment class
    2. Make the bundle within the class by accepting values
    3. Return a fragment type result

The Factory Method Pattern has the following key advantages:

  1. Complete encapsulation of fragment
  2. Reuse of fragment across application activities
  3. We can overload the newInstance() method to make fragments as flexible as possible

These advantages have a huge impact on the maintainability of the code in general.


For those writing their source in java, Android studio has a template in place for writing the newInstance() function. For the kotlin team, follow this example:


package com.cookytech.firstfragment

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View

class MainActivity() : AppCompatActivity() {

    val FRAGMENT1_TAG = "fragment1_tag"

    override fun onCreate(savedInstanceState: Bundle?) {

    fun clickHandler(view: View){

        val fragment:SimpleFragment = SimpleFragment().newInstance("Passed to factory method")
                .add(R.id.fragment_container, fragment, FRAGMENT1_TAG)


    fun removeClickHandler(view: View){
        val fragment = supportFragmentManager




package com.cookytech.firstfragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView

 * A simple [Fragment] subclass.
class SimpleFragment : Fragment() {

    companion object {
        public val MESSAGE_KEY = "message_key"

    fun newInstance(message: String): SimpleFragment {
        val args = Bundle()
        args.putString(MESSAGE_KEY, message)

        val fragment = SimpleFragment()
        fragment.arguments = args
        return fragment

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view = inflater?.inflate(R.layout.fragment_simple, container, false)

        val message = arguments.getString(MESSAGE_KEY)
        val tvMessage: TextView = view!!.findViewById(R.id.message)

        return view


Not to mention that I am not providing XML files etc. over here, this is just to show how to solve a very small problem with a really useful programming pattern. The advantages of using kotlin are already visible as we can see:

  1. small code,
  2. no semi colons,
  3. null pointer safety,
  4. no dealing with empty constructors

And thus, the Factory Method Pattern is implemented in Kotlin for Android.



Leave a Comment


Your e-mail address will not be published. Required fields are marked *