Kotlin - idiomatic way to create a Fragment newInstance pattern

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)
    }
}

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


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.


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)
  } 
}