Created
October 23, 2015 11:57
-
-
Save philipesteiff/0a0373fdb5de4d3941b3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 FragmentUtils { | |
public static FragmentTransaction ensureTransaction(final FragmentManager fragmentManager) { | |
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | |
fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_left); | |
return fragmentTransaction; | |
} | |
public static Fragment getFragment(final FragmentManager fragmentManager, final String tag) { | |
return fragmentManager.findFragmentByTag(tag); | |
} | |
public static Fragment getFragment(final FragmentManager fragmentManager, final int id) { | |
return fragmentManager.findFragmentById(id); | |
} | |
public static void attachFragment(final FragmentTransaction fragmentTransaction, final int content, final Fragment fragment, final String tag) { | |
if (fragment != null) { | |
// fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left); | |
if (fragment.isDetached()) { | |
fragmentTransaction.attach(fragment); | |
} else if (!fragment.isAdded()) { | |
fragmentTransaction.add(content, fragment, tag); | |
} | |
} | |
} | |
public static void reAttachFragment(final FragmentTransaction fragmentTransaction, final Fragment fragment) { | |
fragmentTransaction.attach(fragment); | |
} | |
public static void detachFragment(final FragmentTransaction fragmentTransaction, final Fragment fragment) { | |
if (fragment != null && !fragment.isDetached()) { | |
fragmentTransaction.detach(fragment); | |
} | |
} | |
public static void commitTransactions(final FragmentTransaction fragmentTransaction) { | |
if (fragmentTransaction != null && !fragmentTransaction.isEmpty()) { | |
fragmentTransaction.commit(); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public static <T extends Fragment> T findFragment(final FragmentManager fragmentManager, final String tag) { | |
return (T) fragmentManager.findFragmentByTag(tag); | |
} | |
@SuppressWarnings("unchecked") | |
public static <T extends Fragment> T findFragment(final FragmentManager fragmentManager, final int id) { | |
return (T) fragmentManager.findFragmentById(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment