Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

android - NavHostFragment: Reopen changed fragment with navigation drawer

How do I reopen a fragment, that i previously navigated to with the NavHostFragment?

Navigation Drawer:

Home => Animals-Fragment

Fragments:

Animals-Fragment => Bunnies-Fragment => Temporarily change Bunny color to blue from red

Navigation Drawer:

Bunnies-Fragment => Home

Now I want, that when I click on Animals, it shows the Bunnies-Fragment that was altered. This means, I want the already loaded instance of that Fragment. How do I do that?

I'm using following code to swap between Fragments:

    NavHostFragment.findNavController(p)
            .navigate(R.id.action_navigation_animals_to_bunniesFragment, bundle);

my HomeActivity has this in its onCreate():

   AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_animals)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
question from:https://stackoverflow.com/questions/65946192/navhostfragment-reopen-changed-fragment-with-navigation-drawer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

use popup to instead of the destination

    <action
        android:id="@+id/action_ThirdFragment_to_FirstFragment"
        app:popUpTo="@id/FirstFragment" />

to understand popup to I recommend you watch this video

https://www.youtube.com/watch?v=mLfWvSGG5c8

it explains PopUpTo & PopUpToInclusive in such a great way


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...