I have one activity in my android app which handles the replacement of multiple views/fragments. This activity also has a navigation drawer to navigate to top-level fragments. From the top-level fragments you can navigate to detail-level fragments.
The navigation drawer can always be accessed by sliding it in from the left side. If the current fragment is a top-level fragment, the navigation-drawer can also be accessed by the action-bar. If the current fragment is a detail-level fragment, you can navigate back through the action-bar (or by pressing the back-button).
Lets say I have 3 top-level fragments which can be accessed through the navigation drawer.fragment[1]
, fragment[2]
, fragment[3]
From fragment[1]
, you can navigate to the detail-level fragment[1.1]
.
Now the user wants to navigate directly to the top-level fragment[3]
from the current fragment[1.1]
. The user just has to slide in the navigation-drawer and click the item for fragment[3]
.
Now, if the user hits the back button, the application should close (because it navigated to a top-level fragment). So every time the user navigates to a top-level fragment, the backstack should be cleared. To check if I should display the drawer indicator, I am reading the getBackStackEntryCount()-value and compare it with 0.
As a summary. I want to navigate from detail-level fragments to any top-level fragments and clear the backstack.
The problem:
When I clear the backstack by executinggetSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
,
the fragment transactions are replayed in reverse (for example the replacement of fragment[1]
by fragment[1.1]
). And I DON'T want them to be replayed. I have a lot of initializations in onCreateView, onViewCreated, onStart, .., also starting tasks to fetch data. And I want to skip that all. Just replace the detail level view with a new top-level one which wasn't already in the backstack.
I couldn't find any solution to clear the backstack without popping transactions.
Is there a clean solution to handle this problem? Do I have to implement my own backstack-behavior?
EDIT: The YouTube app also behaves like this. When you navigate from detail to top-level, the backstack gets cleared. But how do they clear the backstack without replaying the fragment-transactions?
Thanks for your answers.
I think you problem is that you are popping fragments in a loop - I guess. I usually push fragments on the fragment stack together with specifying custom animations - say slide in new fragment from the right and when popping it our slid out to the right. Now - if your user has navigated to fragment 1 then 1.1 then 1.2 etc and you want to navigate back to 1 when a user clicks the home button in the action bar - specify a name for you fragment 1 transaction. The you you perform popping use that name. Also consider using popBackStack function instead of popBackStackImmediate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With