Created
January 11, 2018 14:03
-
-
Save aftabsikander/df8bc22959f901e876eaa390210f9ffa to your computer and use it in GitHub Desktop.
Fragment Util
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
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment