Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.app.example.util;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.support.v7.app.AppCompatActivity;
- import com.app.advanceautoparts.R;
- public class FragmentUtils {
- public static boolean sDisableFragmentAnimations = false;
- /**
- * Push fragment into container
- *
- * @param activity required to get fragment manager
- * @param containerId id of container where fragment will be added
- * @param fragment fragment to add
- * @param bundle data for fragment
- * @param tag name of fragment to add
- * @param addToBackStack true if you want to add in back stack
- * @param animate fragment
- */
- public static void pushFragment(FragmentActivity activity, int containerId, Fragment fragment,
- Bundle bundle, String tag, boolean addToBackStack,
- boolean animate) {
- try {
- if (fragment == null) {
- return;
- }
- if (bundle != null) {
- fragment.setArguments(bundle);
- }
- FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
- if (animate) {
- ft.setCustomAnimations(R.anim.slide_in_right
- , R.anim.slide_out_left
- , R.anim.slide_in_left
- , R.anim.slide_out_right);
- }
- if (addToBackStack) {
- ft.addToBackStack(null);
- }
- if (!fragment.isAdded()) {
- ft.replace(containerId, fragment, tag).commit();
- }
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Remove added fragment from specific container id
- *
- * @param activity to get Fragment Manager
- * @param containerId of frame layout
- */
- public static void removeFragmentFromContainer(AppCompatActivity activity, int containerId) {
- FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
- if (ft != null) {
- ft.remove(activity.getSupportFragmentManager().findFragmentById(containerId));
- ft.commit();
- }
- }
- /**
- * Removes all fragments from backstack
- *
- * @param activity required to get fragment manager
- */
- public static void removeAllFragmentsFromBackStack(AppCompatActivity activity) {
- FragmentManager fragmentManager = activity.getSupportFragmentManager();
- FragmentUtils.sDisableFragmentAnimations = true;
- if (fragmentManager != null) {
- int count = fragmentManager.getBackStackEntryCount();
- for (int i = count; i >= 0; i--) {
- fragmentManager.popBackStackImmediate(null,
- FragmentManager.POP_BACK_STACK_INCLUSIVE);
- // fragmentManager.popBackStack();
- }
- }
- }
- /**
- * Checks if specific fragment is in back stack
- *
- * @param activity required to get fragment manager
- * @param tag of fragment to find
- * @return
- */
- public static boolean isFragmentInStack(AppCompatActivity activity, String tag) {
- boolean inStack = false;
- FragmentManager fragmentManager = activity.getSupportFragmentManager();
- if (fragmentManager != null) {
- Fragment fragment = fragmentManager.findFragmentByTag(tag);
- if (fragment != null) {
- inStack = true;
- }
- }
- return inStack;
- }
- public static Fragment getFragmentByTag(AppCompatActivity activity, String tag) {
- FragmentManager fragmentManager = activity.getSupportFragmentManager();
- if (fragmentManager != null) {
- Fragment fragment = fragmentManager.findFragmentByTag(tag);
- if (fragment != null)
- return fragment;
- }
- return null;
- }
- public static void removeFragmentByTag(AppCompatActivity activity, String tag) {
- FragmentManager fragmentManager = activity.getSupportFragmentManager();
- if (fragmentManager != null) {
- Fragment fragment = fragmentManager.findFragmentByTag(tag);
- if (fragment != null)
- fragmentManager.beginTransaction().remove(fragment).commit();
- }
- }
- }
Add Comment
Please, Sign In to add comment