Skip to content

Instantly share code, notes, and snippets.

@amilcar-sr
Last active May 23, 2018 02:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amilcar-sr/7ca29136f63898885db65412b6dff6f2 to your computer and use it in GitHub Desktop.
Save amilcar-sr/7ca29136f63898885db65412b6dff6f2 to your computer and use it in GitHub Desktop.
Utility class that helps with fragment manipulation
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
/**
* Set of methods that help with fragment manipulation.
*/
public class FragmentUtils {
private final static String TAG = "FragmentUtils";
//FragmentManager to be used in case the class has been instantiated.
private FragmentManager mFragmentManager;
/**
* Constructor
*
* @param fragmentManager FragmentManager to be used in case the developer
* instantiates this class
*/
public FragmentUtils(@NonNull FragmentManager fragmentManager) {
mFragmentManager = fragmentManager;
}
/**
* Adds a fragment into the desired container.
*
* @param containerId Id of the container
* @param fragment Fragment to be added
* @param tag Fragment tag
*/
public void addFragment(int containerId, Fragment fragment, String tag) {
addFragment(mFragmentManager, containerId, fragment, tag);
}
/**
* Replaces the container content with the desired fragment.
*
* @param containerId Id of the container
* @param fragment Fragment that will replace the current content
* @param tag Fragment tag
*/
public void replaceFragment(int containerId, Fragment fragment, String tag) {
replaceFragment(mFragmentManager, containerId, fragment, tag);
}
/**
* Removes a fragment by tag
* =
*
* @param tag Fragment tag
*/
public void removeFragment(String tag) {
removeFragment(mFragmentManager, tag);
}
/**
* Returns a fragment of the specified type if found in the fragment manager and is instance of the desired type.
* =
*
* @param fragmentClass Fragment type
* @param tag Fragment tag
* @param <T> Fragment type
* @return Fragment of T type
*/
public <T> T getFragmentByTag(Class<T> fragmentClass, String tag) {
return getFragmentByTag(mFragmentManager, fragmentClass, tag);
}
/**
* Adds a fragment into the desired container.
*
* @param manager Fragment Manager
* @param containerId Id of the container
* @param fragment Fragment to be added
* @param tag Fragment tag
*/
public static void addFragment(FragmentManager manager, int containerId, Fragment fragment, String tag) {
if (!manager.isStateSaved()) {
manager.beginTransaction().add(containerId, fragment, tag).commit();
}
}
/**
* Replaces the container content with the desired fragment.
*
* @param manager Fragment Manager
* @param containerId Id of the container
* @param fragment Fragment that will replace the current content
* @param tag Fragment tag
*/
public static void replaceFragment(FragmentManager manager, int containerId, Fragment fragment, String tag) {
if (!manager.isStateSaved()) {
manager.beginTransaction().replace(containerId, fragment, tag).addToBackStack(tag).commit();
}
}
/**
* Removes a fragment by tag
*
* @param manager Fragment Manager
* @param tag Fragment tag
*/
public static void removeFragment(FragmentManager manager, String tag) {
Fragment fragment = manager.findFragmentByTag(tag);
if (!manager.isStateSaved() && fragment != null) {
manager.beginTransaction().remove(fragment).commit();
}
}
/**
* Returns a fragment of the specified type if found in the fragment manager and is instance of the desired type.
*
* @param manager Fragment Manager
* @param fragmentClass Fragment type
* @param tag Fragment tag
* @param <T> Fragment type
* @return Fragment of T type
*/
public static <T> T getFragmentByTag(FragmentManager manager, Class<T> fragmentClass, String tag) {
Fragment fragment = manager.findFragmentByTag(tag);
if (fragment != null) {
if (fragment.getClass().isAssignableFrom(fragmentClass)) {
return fragmentClass.cast(fragment);
} else if (fragment.getClass().getSuperclass().isAssignableFrom(fragmentClass)) {
return (T) fragment;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment