0

I am trying to pass data from a destination Fragment's FloatingActionButton's OnClickListener to a start Fragment while using a NavController. The FloatingActionButton is in the Activity hosting all the Fragments.

Currently, I can return to the previous Fragment from the FloatingActionButton's OnClickListener like this:

                Bundle bundle = new Bundle();
                bundle.putSerializable(BUNDLE_KEY_SELECTED, selected);
                NavController navController = NavHostFragment.findNavController(FragmentSelectSongs.this);
                navController.popBackStack();

I want to send that Bundle to the start Fragment though. The docs here, Pass data to the start destination, say to set a new graph and add the Bundle. This destroys the backstack though. Even if I do this:

                NavController navController = NavHostFragment.findNavController(FragmentSelectSongs.this);
                navController.setGraph(navController.getGraph(), bundle);
                navController.popBackStack();

This takes me back to the start fragment. How do I handle this?

Should I make an action from destination to start and pass another value, and then pop the backstack twice?

John Glen
  • 771
  • 7
  • 24
  • 1
    If you're trying to return a result to a fragment already on the back stack, is there a reason you're not using the [Navigation API for returning a result](https://developer.android.com/guide/navigation/navigation-programmatic#returning_a_result)? – ianhanniballake Sep 27 '20 at 18:11
  • Because "pass data between fragments" seemed like the most likely place to find the answer! Thank you for the suggestion. I decided to just store the variable in the main activity and avoid the hassle of Android's solutions. – John Glen Sep 27 '20 at 19:40

1 Answers1

1

Did you try this ?

navController.navigate(R.id.fragment_id_to_send_bundle, bundle);
Naveen Rao
  • 712
  • 6
  • 10
  • Yea. I get an IllegalArgumentException due to required arguments not having a default value. Problem is when I give the arguments default values, the actionFragmentAToFragmentB() method generated by safeargs becomes a method with 0 arguments and I can no longer pass the arguments when needed. Is there a way to pass arguments with navigateTo()? – John Glen Sep 27 '20 at 16:00
  • I figured it out, but is there some place where the string values for the arguments are stored? I am using hard coded values now. navigateTo() did work. It does add an extra step to the backstack though, so I am hoping popping the stack twice will fix that. – John Glen Sep 27 '20 at 16:05
  • It worked! I had to use navigate(R.id.fragment_id_to_send_bundle, bundle) instead of navigateTo(R.id.fragment_id_to_send_bundle, bundle), and make sure to add the required arguments to the Bundle. – John Glen Sep 27 '20 at 16:11
  • Oh sorry I haven't checked the method name but was sure that it was something like this. Updated my answer – Naveen Rao Sep 27 '20 at 16:43
  • 1
    Note that for Safe Args generated classes, arguments with default values create `set` methods - you'd do `actionFragmentAToFragmentB().setArg(whatever)` – ianhanniballake Sep 27 '20 at 18:10
  • That only works if I have an action from the destination to the start. That could work as well, but the proposed solutions leave the stack in an unacceptable state and popping the stack twice makes me lose the data, so I decided to store the variable in the main Activity. – John Glen Sep 27 '20 at 19:44
  • I think navigation architecture handles every case. It's not recommended to store data into activity. Sorrry but I'm unable to understand your requirements. – Naveen Rao Sep 28 '20 at 02:06
  • Why is it not recommended to store data in an activity? I can save it during lifecycle transitions. – John Glen Sep 28 '20 at 21:30
  • That variable will be in memory until the activity is not destroyed. Using navigation architecture component using `Bundle`, variables will be using memory only when the fragments using these bundles are in memory. – Naveen Rao Sep 29 '20 at 05:53
  • There is one ArrayList in the Activity used for a few fragments to communicate. I can set it to null when they are done with it. Then the garbage collector eats it. Is there any reason not to store the ArrayList? – John Glen Sep 29 '20 at 14:29