Android - Back Button And Fragment Backstack Not Working
I'm developing a simple fragment-based application with a singe FragmentActivity. Each 'screen' of the application is contained in a fragment and all fragments are added to the co
Solution 1:
Check if you are using FragmentActivity(from support library) instead of Activity. This will cause backstack and transition problem.
Solution 2:
I'm not sure if this will solve your problem but I dont think you need to add all the fragments to begin with.
I've also noticed (at least with the compatibility library) that the replace method seems to be very buggy, so best to remove the existing fragment first and then add the new one.
Here is the bit of code I use to change a fragment:
/**
* Changes the detail fragment of this activity. This is how all content is presented in this app.
* @param fragment
* @param animated
* @param addCurrentFragmentToBackStack
*/privatevoidchangeDetailFragment(Fragment fragment,boolean animated,boolean addCurrentFragmentToBackStack)
{
FragmentTransactiontransaction= getSupportFragmentManager().beginTransaction();
if (animated)
transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
FragmentcurrentFrag= getSupportFragmentManager().findFragmentById(R.id.detailFragment);
StringfragName="NONE";
if (currentFrag!=null)
fragName = currentFrag.getClass().getSimpleName();
if (currentFrag != null)
{
transaction.remove(currentFrag);
}
transaction.add(R.id.detailFragment,fragment);
if (addCurrentFragmentToBackStack)
{
Log.i("APP_NAME","Adding: " + fragName + " to the backstack");
transaction.addToBackStack(null);
}
else
{
Log.i("APP_NAME","Not adding: " + fragName + " to the backstack");
}
transaction.commit();
}
Hope this helps.
Solution 3:
For me changing the version of appcompat to 27.1.1(As of 15/08/2018) worked. Turns out appcompat version v7:28.0.0-rc01 was the culprit.
Post a Comment for "Android - Back Button And Fragment Backstack Not Working"