Created
September 30, 2013 06:09
-
-
Save jfsso/6759888 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onBackPressed() { | |
if (!returnBackStackImmediate(getSupportFragmentManager())) { | |
super.onBackPressed(); | |
} | |
} | |
// HACK: propagate back button press to child fragments. | |
// This might not work properly when you have multiple fragments adding multiple children to the backstack. | |
// (in our case, only one child fragments adds fragments to the backstack, so we're fine with this) | |
private boolean returnBackStackImmediate(FragmentManager fm) { | |
List<Fragment> fragments = fm.getFragments(); | |
if (fragments != null && fragments.size() > 0) { | |
for (Fragment fragment : fragments) { | |
if (fragment.getChildFragmentManager().getBackStackEntryCount() > 0) { | |
if (fragment.getChildFragmentManager().popBackStackImmediate()) { | |
return true; | |
} else { | |
return returnBackStackImmediate(fragment.getChildFragmentManager()); | |
} | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment