I'm fairly new to Android development and now running in to weird behaviour.
Here is my code
public void showFragment(View v) {
FragmentA f = new FragmentA();
FragmentManager fm = getSupportFragmentManager();
String tag = f.getFragmentTag(); // instance method of a to get a tag
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_top, 0, 0, R.anim.slide_out_top);
ft.replace(R.id.container, f, tag);
ft.addToBackStack(tag);
ft.commit();
}
@Override
public void onBackPressed() {
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
} else {
super.onBackPressed();
}
}
When user press button for the first time, it behave like what I expected, add new fragment to container. But, the second time user press button while container still contains a fragment, instead of replacing, it add new one on top of existing one. So, 2 fragments inside container, 2 back press to remove all fragment.
I found that if I remove the line
ft.addToBackStack();
And rework the onBackPress() method like following, it works again like I expected (1 fragment in container at a time)
basically, manually remove fragment instead of popFromBackStack method
private FragmentA currentFragment = null; // to hold the reference to exising fragment, if any.
@Override
public void onBackPressed() {
if (currentFragment != null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(0, R.anim.slide_out_top);
ft.remove(currentFragment);
ft.commit();
currentFragment = null;
} else {
super.onBackPressed();
}
}
So, my question are
Appreciate all comments and suggestions.
addToBackstack
creates a snapshot of your fragments state. Which means when you press the back button, you are actually reverting to the last state that addToBackstack
was called on.
In your case, you add a Fragment. The back button would remove this added fragment. When you call replace, and add to backstack again, you now have two states on the backstack (1. when you had the first fragment added, 2. when you had no fragments added). If you the back button to remove the current fragment, the don't use addToBackstack. Only use addToBackstack when you want to preserve the state of fragments in a view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With