Equivalent of startActivityForResult() with Android Architecture Navigation

In the 1.3.0-alpha04 version of AndroidX Fragment library they introduced new APIs that allow passing data between Fragments.

Added support for passing results between two Fragments via new APIs on FragmentManager. This works for hierarchy fragments (parent/child), DialogFragments, and fragments in Navigation and ensures that results are only sent to your Fragment while it is at least STARTED. (b/149787344)

FragmentManager gained two new methods:

  • FragmentManager#setFragmentResult(String, Bundle) which you can treat similiarly to the existing Activity#setResult ;
  • FragmentManager#setFragmentResultListener(String, LifecycleOwner, FragmentResultListener) which allows you to listen/observe result changes.

How to use it?

In FragmentA add FragmentResultListener to the FragmentManager in the onCreate method:

setFragmentResultListener("request_key") { requestKey: String, bundle: Bundle ->
    val result = bundle.getString("your_data_key")
    // do something with the result
}

In FragmentB add this code to return the result:

val result = Bundle().apply {
    putString("your_data_key", "Hello!")
}
setFragmentResult("request_key", result)

Start FragmentB e.g.: by using:

findNavController().navigate(NavGraphDirections.yourActionToFragmentB())

To close/finish FragmentB call:

findNavController().navigateUp()

Now, your FragmentResultListener should be notified and you should receive your result.

(I’m using fragment-ktx to simplify the code above)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)