steventnorris steventnorris - 1 month ago 6x
Android Question

Bring Fragment to top of stack

When adding activities to the stack, I can do something like this suggests:
How to bring an activity to foreground (top of stack)?

However, I have a Navigation Drawer that uses fragments. I add these fragments to my back stack via the below code:

FragmentTransaction transaction = activity.getFragmentManager().beginTransaction();
transaction.replace(, new EntryFragment());

The problem is, I now need to take a fragment that is already part of the back stack and bring it to the top, dropping all fragments currently above it out of the stack. Essentially what
flags would do when using activities.

How do I accomplish this with fragments?


You can use the following method to return to the instance of the Fragment on the backstack:

activity.getFragmentManager().popBackStackImmediate(tag, 0);

Note, that in your FragmentTransaction you will need to define a unique tag for each Fragment you commit to the backstack and retrieve that tag to return to the fragment here.