Skip to content

Instantly share code, notes, and snippets.

@crgg
Forked from javymarmol/restoringFragment.java
Created May 10, 2018 16:04
Show Gist options
  • Save crgg/7f231d58ecd6c99ccfcbbd770573310c to your computer and use it in GitHub Desktop.
Save crgg/7f231d58ecd6c99ccfcbbd770573310c to your computer and use it in GitHub Desktop.
restore existing fragment
private void changeFragment(Fragment f, String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (fragmentManager.findFragmentByTag(tag) == null) {
fragmentTransaction.add(R.id.frameContainer, f, tag);
} else {
f = fragmentManager.findFragmentByTag(tag);
fragmentTransaction.replace(R.id.frameContainer, f, tag);
}
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment