Search code examples
androidandroid-fragmentsandroid-navigation-graph

How do you pass data to a start Fragment from a destination Fragment's FloatingActionButton's OnClickListener when using a NavController?


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?


Solution

  • Did you try this ?

    navController.navigate(R.id.fragment_id_to_send_bundle, bundle);