I wanted to know when a Fragment has been pop from backstack.
I have my code as below
private void setupChildFragmentPopListener() {
getChildFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
// Do whatever I want
}
});
}
However, this code got triggered both when add and pop fragment. How could I have a way to listen only to pop fragment? Or a way to filter out in the onBackStackChanged()
function if it is not pop?
You can keep a count of the backstack entries and compare it everytime your onBackStackChanged()
is called.
private void setupChildFragmentPopListener() {
getChildFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
int backCount = getChildFragmentManager().getBackStackEntryCount();
}
});
}
For Kotlin lovers:
supportFragmentManager.addOnBackStackChangedListener {
if (supportFragmentManager.backStackEntryCount == 0) {
fab.visibility = VISIBLE
}
// Other magic
}
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