I have total of 7 fragments
Frag1 -> Frag2 -> Frag3 -> Frag4 -> Frag5 -> Frag6 -> Frag7
The user navigates from Frag1 -> Frag7
So, when the user is on Frag7 and presses the back button Frag1 should be visible.
This is fragment transaction code
public void displayView(Fragment fragment, Bundle data) {
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
if (data != null) {
if (fragment.getArguments() != null) {
fragment.getArguments().putAll(data);
} else {
fragment.setArguments(data);
}
}
if (!fragment.isAdded()) {
ft.replace(R.id.frame, fragment);
ft.addToBackStack(null);
}
ft.commit();
} else {
Logger.e("MainActivity", "Error in creating fragment");
}
}
I called
getActivity().getSupportFragmentManager().popBackStack();
5 times before moving to Frag7, but I always get this as backstack
Frag7 -> Frag6 -> Frag1
I am unable to remove the current fragment (Frag6) from where the user navigates.
pls try below logic when u r going from
Frag1 -> Frag2
pls useft.addToBackStack(null);
and after that when u r navigating from
u did not need to add to backstack means do not call add to backStack();