Equivalent of startActivityForResult() with Android Architecture Navigation

AndroidAndroid Architecture-Navigation

Android Problem Overview


I have a workflow with 3 screens. From "screen 1" to access to "screen 2", the user must accept some kind of terms and conditions that I call in my picture "modal". But he only has to accept those conditions once. The next time he is on the first screen, he can go directly to screen 2. The user can chose to NOT accept the terms, and therefore we go back to "screen 1" and do not try to go to "screen 2".

App workflow

I am wondering how to do it with the new navigation component.

Previously, what I would do it:

  • On screen 1, check if the user must accept the conditions
  • If no, start "screen 2" activity
  • If yes, use startActivityForResult() and wait result from the modal. Mark the terms as accepted. Start "screen 2"

But with the navigation graph, there is no way to start a Fragment to obtain a result.

I could mark the terms as accepted in the "modal" screen and start the "screen 2" from there. The thing is that to access to the screen 2, I need to do a network request. I do not want to duplicate the call to the API and processing its outcome in both "screen 1" and "modal".

Is there a way to go back from "modal" to "screen 1" with some information (user accepted the terms), using Jetpack navigation?

Edit: I currently get around it by using the same flow that Yahya suggests below: using an Activity just for the modal and using startActivityForResult from the "screen 1". I am just wondering if I could continue to use the navigation graph for the whole flow.

Android Solutions


Solution 1 - Android

There are a couple of alternatives to the shared view model.

  1. fun navigateBackWithResult(result: Bundle) as explained here https://medium.com/google-developer-experts/using-navigation-architecture-component-in-a-large-banking-app-ac84936a42c2

  2. Create a callback.

ResultCallback.kt

interface ResultCallback : Serializable {
    fun setResult(result: Result)
}

Pass this callback as an argument (note it has to implement Serializable and the interface needs to be declared in its own file.)

<argument android:name="callback"
                  app:argType="com.yourpackage.to.ResultCallback"/>

Make framgent A implement ResultCallback, fragment B by will get the arguments and pass the data back through them, args.callback.setResult(x)

Solution 2 - Android

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:

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)

Solution 3 - Android

It looks like there isn't equivalent for startActivityForResult in Navigation Component right now. But if you're using LiveData and ViewModel you may be interested in this article. Author is using activity scoped ViewModel and LiveData to achieve this for fragments.

Solution 4 - Android

Recently (in androidx-navigation-2.3.0-alpha02 ) Google was released a correct way for achieve this behaviour with fragments.

In short: (from release note)

If Fragment A needs a result from Fragment B..

A should get the savedStateHandle from the currentBackStackEntry, call getLiveData providing a key and observe the result.

findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<Type>("key")?.observe(
    viewLifecycleOwner) {result ->
    // Do something with the result.
}

B should get the savedStateHandle from the previousBackStackEntry, and set the result with the same key as the LiveData in A

findNavController().previousBackStackEntry?.savedStateHandle?.set("key", result)

Related documentation

Solution 5 - Android

There is another alternative workaround. You can use another navigation action from your modal back to screen1, instead of using popBackStack(). On that action you can send whatever data you like to screen one. Use this strategy to make sure the modal screen isn't then kept in the navigation back stack: https://stackoverflow.com/a/54015319/4672107.

The only issue I see with this strategy is that pressing the back button will not send any data back, however most use cases require navigation after a specific user action and and in those situations, this workaround will work.

Solution 6 - Android

To get the caller fragment, use something like fragmentManager.putFragment(args, TargetFragment.EXTRA_CALLER, this), and then in the target fragment get the caller fragment using

if (args.containsKey(EXTRA_CALLER)) {
    caller = fragmentManager?.getFragment(args, EXTRA_CALLER)
    if (caller != null) {
        if (caller is ResultCallback) {
            this.callback = caller
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonas SchmidView Question on Stackoverflow
Solution 1 - AndroidAntPachonView Answer on Stackoverflow
Solution 2 - AndroidZiemView Answer on Stackoverflow
Solution 3 - AndroidLaVepeView Answer on Stackoverflow
Solution 4 - AndroidJuis KelView Answer on Stackoverflow
Solution 5 - AndroidCarson HolzheimerView Answer on Stackoverflow
Solution 6 - AndroidPnemonicView Answer on Stackoverflow