Skip to content

Instantly share code, notes, and snippets.

@miva2
Created September 23, 2015 09:28
Show Gist options
  • Save miva2/b8581f8756a99a204502 to your computer and use it in GitHub Desktop.
Save miva2/b8581f8756a99a204502 to your computer and use it in GitHub Desktop.
Snippet of a BaseActivity class extended by child Activities. Makes it simpler to switch fragments in the same Activity if the setup is always the same. Removes repetition.
protected void switchToFragment(Fragment fragment) {
switchToFragment(fragment, false, null);
}
protected void switchToFragment(Fragment fragment, boolean addToBackStack, String tag) {
if (fragment != null) {
FragmentTransaction transaction = getFragmentManager().beginTransaction().replace(R.id.container, fragment);
if (addToBackStack) { transaction.addToBackStack(tag); }
transaction.commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment