Skip to content

Instantly share code, notes, and snippets.

@bherbst
Last active September 4, 2018 13:56
Show Gist options
  • Save bherbst/6266cb96a6751a4c9d1a6c03db4aee3f to your computer and use it in GitHub Desktop.
Save bherbst/6266cb96a6751a4c9d1a6c03db4aee3f to your computer and use it in GitHub Desktop.
Good backstack
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