How to pass and get value from fragment and activity

66,348

Solution 1

Here is the Android Studio proposed solution (= when you create a Blank-Fragment with File -> New -> Fragment -> Fragment(Blank) and you check "include fragment factory methods").

Put this in your Fragment:

class MyFragment: Fragment {

...

    companion object {

            @JvmStatic
            fun newInstance(isMyBoolean: Boolean) = MyFragment().apply {
                arguments = Bundle().apply {
                    putBoolean("REPLACE WITH A STRING CONSTANT", isMyBoolean)
                }
            }
     }
}

.apply is a nice trick to set data when an object is created, or as they state here:

Calls the specified function [block] with this value as its receiver and returns this value.

Then in your Activity or Fragment do:

val fragment = MyFragment.newInstance(false)
... // transaction stuff happening here

and read the Arguments in your Fragment such as:

private var isMyBoolean = false

override fun onAttach(context: Context?) {
    super.onAttach(context)
    arguments?.getBoolean("REPLACE WITH A STRING CONSTANT")?.let {
        isMyBoolean = it
    }
}

Enjoy the magic of Kotlin!

Solution 2

There is the companion object for that (https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects )

Define your fragment as usual, and declare the companion that acts as the static newInstance() equivalent in Java :

class ViewStackListFragment : Fragment() {
  companion object {
        fun newInstance(position: Int): ViewStackListFragment {
            val fragment = ViewStackListFragment()
            val args = Bundle()
            args.putInt("position", position)
            fragment.setArguments(args)
            return fragment
        }
    }
}

And simply call it like in Java :

val fragment = ViewStackListFragment.newInstance(4)

Solution 3

use this to send arguments to fragment

fun newInstance(index: Int): MyFragment {
    val f = MyFragment ()
    // Pass index input as an argument.
    val args = Bundle()
    args.putInt("index", index)
    f.setArguments(args)
    return f
}

And get those arguments like this

val args = arguments
val index = args.getInt("index", 0)

Solution 4

Kotlin, Fragment: for pass

companion object {
    private const val ARGUMENT_ACTION = "ARGUMENT_ACTION"

    fun newInstance(action: Int) : MyFragment{
        return MyFragment().apply {
            arguments = bundleOf(ARGUMENT_ACTION to action)
        }
    }
}

for get

requireArguments().getInt(ARGUMENT_ACTION)

Solution 5

Do it in more Kotlin style

1) Create an inline function:

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

2) Now you can use this extension in all fragments without duplication of code:

class MyFragment: Fragment() {
    companion object {
        fun newInstance(name: String) = MyFragment().putArgs {
             putString("nameKey", name)
        }
    }
}

class MyFragment1: Fragment() {
     companion object {
         fun newInstance(boolean: Boolean) = MyFragment1().putArgs {
              putBoolean("booleanKey", boolean)
         }
     }
}

3) Create your fragments:

val myFragment = MyFragment.newInstance("NAME")
val myFragment1 = MyFragment1.newInstance(true)
Share:
66,348
Best  Best
Author by

Best Best

Updated on July 05, 2022

Comments

  • Best  Best
    Best Best almost 2 years

    How to pass and get value from fragment and activity?