Created
December 1, 2017 22:27
-
-
Save arvkmr/993e94100c393b6a3da03d951cc085aa to your computer and use it in GitHub Desktop.
Auto back management for fragments with child fragments up to 1 level depth
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() { | |
FragmentManager fm = getSupportFragmentManager(); | |
for (Fragment frag : fm.getFragments()) { | |
if (frag.isVisible()) { | |
FragmentManager childFm = frag.getChildFragmentManager(); | |
if (childFm.getBackStackEntryCount() > 0) { | |
for (Fragment childfragnested: childFm.getFragments()) { | |
FragmentManager childFmNestManager = childfragnested.getFragmentManager(); | |
if(childfragnested.isVisible()) { | |
childFmNestManager.popBackStack(); | |
return; | |
} | |
} | |
} | |
} | |
} | |
super.onBackPressed(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment