Regularly who determines the transaction of Fragments is the Activity, the simplest option is to call the onBackPressed()
of the Activity
that contains the Fragment
:
getActivity().onBackPressed();
and in the onBackPressed()
of the Activity method you obtain the Fragment that was added to the BackStack.
@Override
public void onBackPressed()
{
if(getFragmentManager().getBackStackEntryCount() > 0)
getFragmentManager().popBackStack();
else
super.onBackPressed();
}
For this to work, remember to add the fragments to the BackStack with addToBackStack()
:
fragmentManager.beginTransaction().replace(R.id.home_container, frag).addToBackStack(null).commit();