Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pop back stack for Activity with multiple Fragments?

Activity layout picture

Assume I have an Activity which contains two FrameLayouts (let's call them FrameA and FrameB) which in turn each contain a Fragment (let's call them FragmentA1 and FragmentB1 respectively). Now, I commit a series of individual fragment transactions using code similar to the following...

getFragmentManager()  .beginTransaction()  .replace(frameId, fragment)  .addToBackStack(null)  .commit(); 

... such that I replace FragmentA1 in FrameA with FragmentA2, then I replace FragmentB1 in FrameB with FragmentB2, then I replace FragmentA2 in FrameA with FragmentA3, then I replace FragmentB2 in Frame2 with FragmentB3, and the final state looks like the picture above (where only FragmentA3 and FragmentB3 are visible).

If I understood correctly how the back stack works, pressing 'back' will interleave popping of the Fragments between FrameA and FrameB (reflecting how I added them).

Does anyone know if it is possible to pop the last transaction on FrameA or FrameB selectively? (i.e. if I pressed 'Pop FrameA' then FrameA would be transitioned back from FragmentA3 to FragmentA2 and, instead, if I pressed 'Pop FrameB' then FrameB would be transitioned back from FragmentB3 to FragmentB2)

Supplement: I know I can get the Fragment last added to a given FrameLayout using the FragmentManager.findFragmentById(int framelayoutId) method, but calling FragmentTransaction.remove(fragment).commit() only removes the Fragment from the View and does not transition the View back to the Fragment it previously displayed.

like image 383
Adil Hussain Avatar asked Jan 07 '12 20:01

Adil Hussain


People also ask

Can one activity have multiple fragments?

One Html can contain multiple Frames, and one Activity can include multiple Fragments. Each Fragment can have its own layout XML file, can contain its own view objects and encapsulate its own functionality.

How do I remove all fragments in the Backstack?

Explanation: MainFragment -> Fragment A -> Fragment B (this is added to backstack) -> Fragment C -> MainFragment (clear backstack ).

How many fragments can there be in one activity?

Two Fragments should never communicate directly. Show activity on this post. There is no official guideline regarding the limit of fragments that can be added to an activity. You can have as many as you need and looks logical for your app.


1 Answers

Basically, no, there is only one back stack for an activity.

You will just need to implement your own separate back stacks.

As of Android 4.0 (and the associated support library) there are APIs that should make this relatively easy -- FragmentTransaction.detach(Fragment) lets you put a fragment into the same state it is when in the back stack, and FragmentManager.saveFragmentInstanceState(Fragment) lets you go further and completely throw away the Fragment object. Not coincidentally, these are used to implement ViewPager's FragmentPagerAdapter and FragmentStatePagerAdapter, respectively, so you could look at the code for these as an example of how to use them.

like image 181
hackbod Avatar answered Sep 20 '22 11:09

hackbod