steventnorris
steventnorris

Reputation: 5896

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(R.id.main_fragment, new EntryFragment());
                transaction.addToBackStack(activity.mTitle.toString());
                transaction.commit();

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 FLAG_ACTIVITY_REORDER_TO_FRONT and FLAG_ACTIVITY_CLEAR_TOP flags would do when using activities.

How do I accomplish this with fragments?

Upvotes: 0

Views: 2478

Answers (1)

Joseph Roque
Joseph Roque

Reputation: 5146

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.

Upvotes: 3

Related Questions