From How to resume a fragment from BackStack, if it exists , which may be very similar to your request.
There is a way to put a back stack based on the name of the transaction or identifier provided by the commit (for example, using the fragment class name as Thijs already mentioned):
String backStateName = fragment.getClass().getName();
Adding to backstack:
FragmentTransaction ft = manager.beginTransaction(); ft.replace(R.id.content_frame, fragment); ft.addToBackStack(backStateName); ft.commit();
When you press:
getSupportFragmentManager().popBackStackImmediate (backStateName, 0);
So, before replacing the fragment, we can check the existence in the backstack, for example:
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0); if (!fragmentPopped){ //fragment not in back stack, create it. ... ... ft.addToBackStack(backStateName); ft.commit(); }
Please check the question above and the accepted answer for a more detailed explanation.
source share