简体   繁体   中英

Navigation with fragments in navigation drawer

Let me describe situtation : I have activity that has navigation drawer that has items A, B, C, D, S(PreferencesFragment). Now as user goes into app he can choose whatever he wants. Lets say he click on B this opens new fragmentB that has list of some items. Clicking on item opens new fragmentDetails and puts transaction of fragments to back stack to enable user to go back to fragmentB with list.
Let user still be on fragmentDetails, if he now chooses to go to fragmentS from nav. drawer I would like the back stack to be empty. Is there any way how to let back stack to forget about remaining transactions ? I dont mean like popBackStack, becouse this would result into for a some small amont of time showing fragmentB.

How to achive this kind of navigation ?

lupajz, please take some time to consider on which fragments the user will have access to the navigation drawer. Most of the time when you reach a "item details" fragment you may wan't to offer a different kind of navigation.

What you want to achieve can be done using this line:

getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

Nevertheless please take a look at the following blog posts I've written covering this issues:

https://aarcoraci.wordpress.com/2017/02/13/android-tutorial-drawer-and-fragment-navigation-made-easyier/

https://aarcoraci.wordpress.com/2017/02/14/android-drawer-and-fragment-navigation-a-more-real-life-scenario/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM