Passing data between Activities, Fragments, and Dialog Fragments

Aaush Shrestha
2 min readMay 12, 2021

--

Passing data from one view to another view (Activities/fragments) is a common use case when it comes to Android applications. This blog article describes simple steps to transfer data from one view to another. We can transfer data in different formats like strings, booleans as well as objects.

We could achieve these with the help of ViewModel too but I tried to achieve that in the very easiest way.

Before we begin you need to know to share the objects you need to add Parcelize in data class. For Example:

@Parcelize
data class ABCModel(
val name: String,
val address: String
):Parcelable

Okay, let’s begin!!

1. Activity to Activity

Before we start let’s know about `lazy` property.

lazy() is a function that takes a lambda and returns an instance of Lazy<T> which can serve as a delegate for implementing a lazy property: the first call to get() executes the lambda passed to lazy() and remembers the result, subsequent calls to get() simply return the remembered result.

On receiver activity, let’s add a companion object to make it easy to start an activity, with passing data.

companion object {private const val NAME = "NAME"private const val ABCMODEL = "ABCMODEL"fun newIntent(  context: Context,  name: String,  abcModel : ABCModel): Intent {return Intent(context, ReceiverActivity::class.java).apply {putExtra(NAME, name)putExtra(ABCMODEL, abcModel)     }   }}

Here, We add extended data to the intent on string value of `DATA` as well as object on `ABCMODEL`.

And to get the intent extra in a receiver activity, add this line,

For String,

private val userName :String? by lazy { intent.getStringExtra("DATA") }

For Object

private val abcModelData: ABCModel? by lazy { intent.getParcelableExtra(ABCMODEL)

Finally, on Sender activity

startAcitvity(ReceiverActivity.newIntent(this, "John Doe", abcModel)

2. Activity to Fragment

In this blog, I am using the `supportFragmentManager.beginTransaction()` for add fragments.

For example,

fun addFragment(fragment: Fragment) {supportFragmentManager.beginTransaction().setCustomAnimations(enter, exit, enter, exit).add(R.id.nav_host_fragment, fragment).addToBackStack(BACK_STACK).commit()}

Now, let’s transfer data from ABCFragment to XYZFragment

in XYZFragment

class XYZFragment(
private val name: String,
private val abcModelData: ABCModel,
) :Fragment()

Now, We can use the “name” and “abcModelData” anywhere inside the fragment.

in ABCFragment

mActivity.addFragment(XYZFragment("John Doe", abcModel))

Here, The “addFragment” function is placed in the main activity(Host activity) as I mention above,

So to access the function we need to add this line

val mActivity by lazy { activity as MainActivity }

3. Fragment/ Activity to Dialog fragment

To transfer data to dialog fragment we can use the same technique which we used for fragments.

here’s the code to show the dialog fragment with passing data.

val dialogFragment = ABCDialogFragment("John Doe", abcModel)showRisk.show(requireActivity().supportFragmentManager,TAG)

Return data to calling view is coming soon. Thank you for the reading :)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Aaush Shrestha
Aaush Shrestha

No responses yet

Write a response