Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove All Previous Fragments from Current Fragment except current one and first one?

I have a Fragment stack like this

F1 -> F2 -> F3 -> F4 -> F5

Now I need to remove F2, F3, F4 fragments.

I need if i press back button from F5 fragment, it should be go to F1.

NOTE: I am not changing fragment fragment from activity. changing fragment from fragment.

like image 967
AvisSiva Avatar asked Jun 11 '16 12:06

AvisSiva


1 Answers

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

like image 93
Rohit Arya Avatar answered Sep 23 '22 06:09

Rohit Arya