I have a probleam and I can't find a solution anywhere.
My app doesn't return to previous fragment when I press the back button, instead closes the activity.
I have an activity that displays 4 fragment: [1],[2],[3],[4]. I can switch between the first 3 fragments with the action bar, I don't want to add them to the back stack.
Fragment [4] is a detailed view of an item selected in fragment [3]. When I press back in [4] I want to return to fragment [3], not closing the app.
The transitions are done in this way BY THE ACTIVITY in which fragments are placed:
private void replaceFragment(Fragment fragment, boolean toBackStack){
if(fragment != null){
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
if(toBackStack)
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
where toBackStack is always false except when the transition is from [3] to [4].
If I pass toBackStack true in every transition, the activity closes anyway.