Last active
August 29, 2015 14:03
-
-
Save dadino/338c5be31e4f385618e7 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
import android.app.Activity; | |
import android.app.Fragment; | |
import android.app.FragmentManager; | |
import android.app.FragmentTransaction; | |
public class FragmentUtils { | |
public static void addFragmentsInActivity(Activity act, | |
int containerId, Fragment fragment, String tag) { | |
FragmentManager fm = act.getFragmentManager(); | |
FragmentTransaction ft = fm.beginTransaction(); | |
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); | |
ft.add(containerId, fragment, tag); | |
ft.commit(); | |
} | |
public static void removeFragmentsInActivity(Activity act, | |
int containerId, Fragment fragment) { | |
FragmentManager fm = act.getFragmentManager(); | |
FragmentTransaction ft = fm.beginTransaction(); | |
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); | |
ft.remove(fragment); | |
ft.commit(); | |
} | |
public static void removeFragmentsInActivity(Activity act, | |
int containerId, String tag) { | |
FragmentManager fm = act.getFragmentManager(); | |
FragmentTransaction ft = fm.beginTransaction(); | |
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); | |
ft.remove(fm.findFragmentByTag(tag)); | |
ft.commit(); | |
} | |
public static void switchFragmentsInActivity(Activity act, | |
int containerId, Fragment newFragment, String tag) { | |
FragmentManager fm = act.getFragmentManager(); | |
FragmentTransaction ft = fm.beginTransaction(); | |
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); | |
ft.replace(containerId, newFragment, tag); | |
ft.commit(); | |
} | |
public static boolean isFragmentVisible(Activity act, String tag) { | |
Fragment myFragment = getFragmentByTag(act, tag); | |
if (myFragment != null) { | |
if (myFragment.isVisible()) { | |
return true; | |
} else { | |
return false; | |
} | |
} else { | |
return false; | |
} | |
} | |
public static boolean isFragmentPresent(Activity act, String tag) { | |
Fragment myFragment = getFragmentByTag(act, tag); | |
if (myFragment != null) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public static Fragment getFragmentByTag(Activity act, String tag) { | |
return act.getFragmentManager().findFragmentByTag(tag); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment