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 executing
getSupportFragmentManager().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.
Related
I have an activity consisting of 3 fragments and a bottom navigation view which navigates the user to different fragments.
Let say the fragments are A, B and C.
The issue I am facing is that if I have performed some operation in A, and I navigate to B, then, when I again navigate to A (from bottom navigation view and NOT backpress) , the operations performed on A are lost, which I don't want.
For eg, in Youtube application, after searching something on Home fragment, if I navigate to "Subscriptions" or "Library", then if I renavigate to "Home", the search results are retained and also the state of the screen.
Kindly guide me how to achieve the same in my application
I have a Navigation panel activity. With 5 fragments (Will name it as Fragment1, Fragment2, ...) in menu sections.
Now by Default, activity will display Fragment1.
If user navigate to Fragmentxtz from Fragment1. We will add the fragment on top of Fragment1.
Now user goes to background by pressing home button and open the app from tasks.
Now i know Fragmentxtz onStart will be called. But i see that Fragment1 onStart is also called.
Is this expected behavior ?
As you can see on Android Developers, your fragment will be called at onViewCreated().
https://developer.android.com/guide/components/fragments#Creating
The View has to be updated in case you changed the system language or something like that.
Providing a bit more context to what might be happening here.
If both of your fragments are added e.g. via FragmentTransaction.add() both of them will have onCreateView() called when the layout is restored for the user as Robert pointed out. From the system's point of view all of those fragments are relevant to the user and would be shown simultaneously.
If on the other hand you add fragments via FragmentTransaction.replace() only the topmost fragment on the back stack will receive the onCreateView() call. This can also be achieved by doing an add and remove of the old fragment. If you make this transaction reversible by the back stack after pressing the back button your previous fragment would receive the appropriate lifecycle callbacks.
I am currently writing a drawer layout as my main layout, with an embedded FrameLayout that I will be using to hold each “page” when an item on the drawer is clicked on. When the app first starts, an initial fragment will be show. Other fragments may be added/replaced later which is fine, however, my problem is that when the user clicks the back button on the very first “initial fragment”, I don’t want that particular fragment to be removed from the layout. Currently, it is being removed and it’s just showing a drawer layout with no other content (which makes sense). I want the app to automatically exit if the initial fragment was the last one showing and the back button is pressed, instead of removing that initial fragment and then after another back press, then it exits.
Things I have thought of doing:
Not adding the first fragment to the backstack. (If I do this, I can compare it with the classname of the fragment which is a somewhat longer string, or I can use a boolean value for once the first fragment has been placed (and not added to backstack), the boolean is set which allows the fragments to now be added.
Overriding the onBackPressed function of the activity
Does anyone have a suggested way of doing this or can think of a better way? Thanks
The first bullet point sounds the cleanest. You have no other need to handle conditions when back is hit, correct? If that's the case, it's less lines of code (removing one as opposed to adding several) and you get to keep default Activity methods as is.
I know that's not exactly what you asked, but I think the first bullet point is so clean, that I just wouldn't try something else.
I have implemented same in one of the app using my own Stack of fragment. and also implemented onBackPressed method.
Every time when user clicks on item in drawer i add fragment in stack and in back press once its length is 1 I finish the activity with message.
On item click -- Add/replace fragment in container.
OnBackPressed -- Pop fragments from stack and once its last one i finish activity.
Hope this can give you another option to consider.
I have 2 fragments(Frag A, Frag B) which are shown in multi pane in landscape with different container id (R.id.containerA,R.id.containerB) respectively.
My screen flow for 1st fragment is FragA->FragA1->FragA2
My screen flow for 2nd fragment is FragB->FragB1
I am adding each fragment to backstack. So I have around 5 fragments in backstack.
Actual order of navigating is FragA->FragB->FragA1->FragB1->FragA2
Now when I press back button, I want FragB1 to be popped out first from backstack instead of FragA2. I know fragments are maintained in a stack but how to handle this particular multi pane scenario ?
Should I use reflection like mentioned in this post ?
Android Reorder Fragment Backstack
Any other alternatives ?
If you simply want to get FragB1 instead of FragA2, you can check by obtaining the name of the fragment and then go one step back if name of the fragment is FragA2. You can get the name using the following code:
FragmentManager.BackStackEntry backEntry=getFragmentManager().getBackStackEntryAt(getActivity().getFragmentManager().getBackStackEntryCount()-1);
String str=backEntry.getName();
I've been struggling with this for a while now, I want to start off with a diagram of my problem:
The three navigation drawer buttons are part of my Base Activity. Each purple block below the three buttons are fragments, and the descendants of each of those blocks are in turn fragments. I'll use the master and detail fragments as a demonstration of the issue I am having...The user clicks the nav drawer button, which opens the master fragment that hosts a list of articles. Once a user clicks one of those articles, I then open the detail fragment inside of the master fragment. So If I find myself at the detail, and I decide to open the nav drawer and click the third button for instance, then click the second button again, I want the detail to be open still, and if I hit the phone's back button I want it to move back to the master fragment, and end there. Any tips will be helpful, as I am probably going to use a similar pattern for the first button, it's main fragment and it's children fragments as well.
In my opinion, the cleanest way to handle what you are describing would be to have three separate FragmentActivity classes that implement the DrawerLayout instead of one monolithic BaseActivity.
Each button in the drawer should start it's respective FragmentActivity using launchMode singleTask. This ensures that you launch the same activity instance each time, instead of a new one, which will maintain your back stack for each activity as you switch between them using the drawer buttons. See Android Developer Guide Activity:launchMode for more details.
Each of the three FragmentActivity instances should be responsible for starting and managing it's fragments using listener interfaces. For example, where you have your Master fragment opening a Detail fragment directly, you should instead have your Master fragment tell it's FragmentActivity that it needs to open the Detail fragment. See Android Developer Guide: Communicating with Other Fragments for recommended practices in implementing this type of decoupled communication between FragmentActivity and Fragment. It will make your life much easier down the road when you want different layouts for tablets, etc.
Each of the three main drawer "tasks" seem to be unique enough that isolating each within it's own FragmentActivity seems the best way to implement what you are trying to do. You can apply this same approach for each of your main sections.
I had the same problem and didn't want to go to multiple activities since that would complicate the back navigation of my application. Your fragments won't save their state automatically unless the activity lifecycle events are being called.
In our case those don't happen since we're not leaving the activity. You can use FragmentManager's saveFragmentState on the fragment you are replacing to manually trigger the state saving and get a Fragment.SavedState object. You can keep a list of your SavedState objects and when pushing a fragment check if you have a saved state for it. If so you can call Fragment setInitialSavedState which will cause your fragment to load the previous state.
Now in my app as a user toggles between fragments with their own child fragments the state is retained when they come back.