0

In my app I have one Activity and the rest are fragments. Right now I am trying to get run as follows. Let's say, I have Fragments A (Home), B, and C. The scenario is this:

I navigate from A to B that shows me some data that I pass from A by Safe args. Then I navigate from B to C to edit data shown in B. Then I go back from C to B and B shows me the edited data in C. Lastly, I go back to home. That's all.

I pass data by Safe args, but the problem is, when I come back to B from C, I cannot navigate back to A because the navigateUp() brings me back to C instead of going to A.

Navigating methods:

  • From A to B: Navigation.findNavController(requireActivity(), R.id.nav_host_fragment).navController().navigate(HomeFragmentDirections.actionNavHomeToNavB(some_string_data))
  • From B to C: Navigation.findNavController(requireActivity(), R.id.nav_host_fragment).navController().navigate(BFragmentDirections.actionNavBToNavC(string_data_in_B))
  • From C to B: Navigation.findNavController(requireActivity(), R.id.nav_host_fragment).navController().navigate(CFragmentDirections.actionNavCToNavB(edited_string_data_from_B))
  • From B to A: Navigation.findNavController(requireActivity(), R.id.nav_host_fragment).navController().navigateUp()

I also tried to use navigateUp() before going back to B, which throws exception that the destination does not exist and also, I tried to use popBackStack() to take the C from stack out so that the navigateUp() in B can bring me back to Home. No success yet.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark Delphi
  • 1,376
  • 1
  • 11
  • 29
  • "From C to B" seems wrong. Try https://developer.android.com/guide/navigation/navigation-programmatic#returning_a_result or a shared `ViewModel`. – CommonsWare Nov 02 '20 at 13:51
  • @CommonsWare Thanks for response. I saw that solution linked above by you, but I can't really understand, how does that work. Also, tried to find an example of a use case. Could you please share an example if you have one or something like that? Thanks. – Mark Delphi Nov 02 '20 at 16:04
  • [This Kotlin sample](https://gitlab.com/commonsguy/cw-jetpack-kotlin/-/tree/v1.1/NukeFromOrbit) and [its Java counterpart](https://gitlab.com/commonsguy/cw-jetpack-java/-/tree/v1.1/NukeFromOrbit) demonstrate a `DialogFragment` returning results via the Navigation component. That's not precisely your scenario, but it's all that I have handy. – CommonsWare Nov 02 '20 at 16:57
  • So, I tested the linked solution as follows: I setup a listener in B and than passed data From B to C and then, I set the result on the `SavedStateHandle` as described in the link. I did everything as linked by @CommonsWare beside passing data from B to C since there is no information about any kind of data passing and that's why I did not have any other choices. As a result, it does not still work... I have no ideas any more. Why is that so difficult to get it run? Why does Google that in that useless way? – Mark Delphi Nov 02 '20 at 18:40
  • "beside passing data from B to C since there is no information about any kind of data passing" -- that sounds like nav arguments. "it does not still work" -- you might consider a separate question with a [mcve]. "Why does Google that in that useless way?" -- I agree that the `SavedStateHandle` is kinda awkward. A shared `ViewModel` would offer greater flexibility, or it may be that the sharing should be deeper in your architecture (e.g., C updates a repository, which winds up updating B via observers in its `ViewModel`). – CommonsWare Nov 02 '20 at 20:40

0 Answers0