Guest User

Untitled

a guest
Jan 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. package com.app.example.util;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v4.app.Fragment;
  5. import android.support.v4.app.FragmentActivity;
  6. import android.support.v4.app.FragmentManager;
  7. import android.support.v4.app.FragmentTransaction;
  8. import android.support.v7.app.AppCompatActivity;
  9.  
  10. import com.app.advanceautoparts.R;
  11.  
  12.  
  13. public class FragmentUtils {
  14. public static boolean sDisableFragmentAnimations = false;
  15.  
  16. /**
  17. * Push fragment into container
  18. *
  19. * @param activity required to get fragment manager
  20. * @param containerId id of container where fragment will be added
  21. * @param fragment fragment to add
  22. * @param bundle data for fragment
  23. * @param tag name of fragment to add
  24. * @param addToBackStack true if you want to add in back stack
  25. * @param animate fragment
  26. */
  27. public static void pushFragment(FragmentActivity activity, int containerId, Fragment fragment,
  28. Bundle bundle, String tag, boolean addToBackStack,
  29. boolean animate) {
  30.  
  31. try {
  32. if (fragment == null) {
  33. return;
  34. }
  35.  
  36. if (bundle != null) {
  37. fragment.setArguments(bundle);
  38. }
  39.  
  40. FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
  41.  
  42. if (animate) {
  43. ft.setCustomAnimations(R.anim.slide_in_right
  44. , R.anim.slide_out_left
  45. , R.anim.slide_in_left
  46. , R.anim.slide_out_right);
  47. }
  48.  
  49. if (addToBackStack) {
  50. ft.addToBackStack(null);
  51. }
  52.  
  53. if (!fragment.isAdded()) {
  54. ft.replace(containerId, fragment, tag).commit();
  55. }
  56.  
  57. } catch (IllegalStateException e) {
  58. e.printStackTrace();
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62.  
  63. }
  64.  
  65. /**
  66. * Remove added fragment from specific container id
  67. *
  68. * @param activity to get Fragment Manager
  69. * @param containerId of frame layout
  70. */
  71. public static void removeFragmentFromContainer(AppCompatActivity activity, int containerId) {
  72. FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
  73. if (ft != null) {
  74. ft.remove(activity.getSupportFragmentManager().findFragmentById(containerId));
  75. ft.commit();
  76. }
  77. }
  78.  
  79. /**
  80. * Removes all fragments from backstack
  81. *
  82. * @param activity required to get fragment manager
  83. */
  84. public static void removeAllFragmentsFromBackStack(AppCompatActivity activity) {
  85. FragmentManager fragmentManager = activity.getSupportFragmentManager();
  86. FragmentUtils.sDisableFragmentAnimations = true;
  87. if (fragmentManager != null) {
  88. int count = fragmentManager.getBackStackEntryCount();
  89. for (int i = count; i >= 0; i--) {
  90. fragmentManager.popBackStackImmediate(null,
  91. FragmentManager.POP_BACK_STACK_INCLUSIVE);
  92.  
  93. // fragmentManager.popBackStack();
  94. }
  95. }
  96. }
  97.  
  98. /**
  99. * Checks if specific fragment is in back stack
  100. *
  101. * @param activity required to get fragment manager
  102. * @param tag of fragment to find
  103. * @return
  104. */
  105. public static boolean isFragmentInStack(AppCompatActivity activity, String tag) {
  106. boolean inStack = false;
  107.  
  108. FragmentManager fragmentManager = activity.getSupportFragmentManager();
  109.  
  110. if (fragmentManager != null) {
  111. Fragment fragment = fragmentManager.findFragmentByTag(tag);
  112. if (fragment != null) {
  113. inStack = true;
  114. }
  115.  
  116. }
  117.  
  118. return inStack;
  119. }
  120.  
  121. public static Fragment getFragmentByTag(AppCompatActivity activity, String tag) {
  122. FragmentManager fragmentManager = activity.getSupportFragmentManager();
  123. if (fragmentManager != null) {
  124. Fragment fragment = fragmentManager.findFragmentByTag(tag);
  125. if (fragment != null)
  126. return fragment;
  127. }
  128. return null;
  129. }
  130.  
  131.  
  132. public static void removeFragmentByTag(AppCompatActivity activity, String tag) {
  133. FragmentManager fragmentManager = activity.getSupportFragmentManager();
  134. if (fragmentManager != null) {
  135. Fragment fragment = fragmentManager.findFragmentByTag(tag);
  136. if (fragment != null)
  137. fragmentManager.beginTransaction().remove(fragment).commit();
  138. }
  139.  
  140. }
  141. }
Add Comment
Please, Sign In to add comment