Last active
September 4, 2018 13:56
-
-
Save bherbst/6266cb96a6751a4c9d1a6c03db4aee3f to your computer and use it in GitHub Desktop.
Good backstack
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
public class MainActivity extends Activity { | |
private static final String BACK_STACK_ROOT_TAG = "root_fragment"; | |
public void onTabSelected(int position) { | |
// Pop off everything up to and including the current tab | |
FragmentManager fragmentManager = getSupportFragmentManager(); | |
fragmentManager.popBackStack(BACK_STACK_ROOT_TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE); | |
// Add the new tab fragment | |
fragmentManager.beginTransaction() | |
.replace(R.id.container, TabFragment.newInstance()) | |
.addToBackStack(BACK_STACK_ROOT_TAG) | |
.commit(); | |
} | |
/** | |
* Add a fragment on top of the current tab | |
*/ | |
public void addFragmentOnTop(Fragment fragment) { | |
getSupportFragmentManager() | |
.beginTransaction() | |
.replace(R.id.container, fragment) | |
.addToBackStack(null) | |
.commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment