triad
triad

Reputation: 21507

Kotlin - idiomatic way to create a Fragment newInstance pattern

The best practice on Android for creating a Fragment is to use a static factory method and pass arguments in a Bundle via setArguments().

In Java, this is done something like:

public class MyFragment extends Fragment {
    static MyFragment newInstance(int foo) {
        Bundle args = new Bundle();
        args.putInt("foo", foo);
        MyFragment fragment = new MyFragment();
        fragment.setArguments(args);
        return fragment;
    }
}

In Kotlin this converts to:

class MyFragment : Fragment() {
    companion object {
       fun newInstance(foo: Int): MyFragment {
            val args = Bundle()
            args.putInt("foo", foo)
            val fragment = MyFragment()
            fragment.arguments = args
            return fragment
        }
    }
}

This makes sense to support interop with Java so it can still be called via MyFragment.newInstance(...), but is there a more idiomatic way to do this in Kotlin if we don't need to worry about Java interop?

Upvotes: 49

Views: 42545

Answers (7)

Vlad
Vlad

Reputation: 8552

More elegant way in my opinion

open class Instance<T : Fragment> {

    @Suppress("UNCHECKED_CAST")
    fun newInstance(vararg args: Pair<String, Any?>): T {
        val cls = Class.forName(javaClass.name.substringBefore("$"))
        return (cls.newInstance() as T).apply {
            arguments = bundleOf(*args)
        }
    }
}

class MyFragment : Fragment() {

    companion object : Instance<MyFragment>()
}

Keep in mind to add proguard rule to save constructor

-keepclassmembers class * extends androidx.fragment.app.Fragment {
   <init>(...);
}

Or without reflection and proguard

open class Instance<T : Fragment>(private val cls: Class<T>) {

    fun newInstance(vararg args: Pair<String, Any?>): T {
        return cls.newInstance().apply {
            arguments = bundleOf(*args)
        }
    }
}

class MyFragment : Fragment() {

    companion object : Instance<MyFragment>(MyFragment::class.java)
}

Example of usage

val myFragment = MyFragment.newInstance("foo" to "bar)

Upvotes: 2

Mustafa G&#252;ven
Mustafa G&#252;ven

Reputation: 15744

companion object {
  private const val NOTE_ID = "NOTE_ID"
  fun newInstance(noteId: Int?) = AddNoteFragment().apply {
  arguments =
      Bundle().apply { putInt(NOTE_ID, noteId ?: Int.MIN_VALUE) }
  }
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  super.onViewCreated(view, savedInstanceState)
  arguments?.let {
    noteId = it.getInt(NOTE_ID)
  } 
}

Upvotes: 12

jt-gilkeson
jt-gilkeson

Reputation: 2721

Late to the party, but I believe Idiomatically it should be something like this:

private const val FOO = "foo"
private const val BAR = "bar"

class MyFragment : Fragment() {
    companion object {
        fun newInstance(foo: Int, bar: String) = MyFragment().withArgs {
            putInt(FOO, foo)
            putString(BAR, bar)
        }
    }
}

With an extension like this:

inline fun <T : Fragment> T.withArgs(argsBuilder: Bundle.() -> Unit): T =
    this.apply {
        arguments = Bundle().apply(argsBuilder)
    }

or

companion object {
    fun newInstance(foo: Int, bar: String) = MyFragment().apply {
        arguments = bundleOf(
            FOO to foo,
            BAR to bar
        )
    }
 } 

The key being that the private constants should not be part of the companion object.

Upvotes: 12

Dmide
Dmide

Reputation: 6462

inline fun <reified T : Fragment>
    newFragmentInstance(vararg params: Pair<String, Any>) =
    T::class.java.newInstance().apply {
        arguments = bundleOf(*params)
    }`

So it is used like that:

val fragment = newFragmentInstance<YourFragment>("key" to value)

Credit

bundleOf() can be taken from Anko

Upvotes: 16

Francesc
Francesc

Reputation: 29260

I like to do it this way:

companion object {
    private const val MY_BOOLEAN = "my_boolean"
    private const val MY_INT = "my_int"

    fun newInstance(aBoolean: Boolean, anInt: Int) = MyFragment().apply {
        arguments = Bundle(2).apply {
            putBoolean(MY_BOOLEAN, aBoolean)
            putInt(MY_INT, anInt)
        }
    }
}

Edit: with KotlinX extensions, you can also do this

companion object {
    private const val MY_BOOLEAN = "my_boolean"
    private const val MY_INT = "my_int"

    fun newInstance(aBoolean: Boolean, anInt: Int) = MyFragment().apply {
        arguments = bundleOf(
            MY_BOOLEAN to aBoolean,
            MY_INT to anInt)
    }
}

Upvotes: 84

Zar E Ahmer
Zar E Ahmer

Reputation: 34360

Another way of doing this I found here

class MyFragment: Fragment(){
  companion object{
    private val ARG_CAUGHT = "myFragment_caught"

    fun newInstance(caught: Pokemon):MyFragment{
      val args: Bundle = Bundle()
      args.putSerializable(ARG_CAUGHT, caught)
      val fragment = MyFragment()
      fragment.arguments = args
      return fragment
    }
    ...
  }
  ...
}

Upvotes: 2

Irving Dev
Irving Dev

Reputation: 399

Kotlin package-level function

What about about that kotlin says to use package level function instead of “static” method

MyFragment.kt

class MyFragment : Fragment() {

    .....

}

fun MyFragmentNewInstance(): MyFragment {
    return MyFragment()
}

MyActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (supportFragmentManager.findFragmentById(R.id.fragmentContainer) == null) {
        supportFragmentManager.beginTransaction()
            .add(R.id.fragmentContainer, MyFragmentNewInstance())
            .commit()
    }
}

Upvotes: 1

Related Questions