How to Remove All Previous Fragments from Current Fragment except current one and first one?
On destroy of the Fragment
F5
clear Back Stack
upto F2
.
Try something like this:
public
method in your MainActivity
:
public void clearBackStackInclusive(String tag) {
getSupportFragmentManager().popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
now in your F5
fragment:
@Override
public void onDestroy() {
super.onDestroy();
((MainActivity)getActivity()).clearBackStackInclusive("tag"); // tag (addToBackStack tag) should be the same which was used while transacting the F2 fragment
}
Reference
You can manage tasks with flags. For example, this will clear all the activities on top of home.
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent)
So if you start 4 or 5 or more tasks over the Home Activity, you can call finish on the fifth and get back home.
I am looking forward for a fragment version of it, but it depends on the way you manage tasks.
I found it for fragments :
FragmentManager.popBackStack(String name, FragmentManager.POP_BACK_STACK_INCLUSIVE)
Which will pop all states up to the named one.