I am trying to manage fragments in a very simple way. I have created a utility class that's adding fragments to the backstack and when we press the back button, the previous fragment shows up. I am OK with that. But, when I am trying to clear out all the fragments, and show up the root fragment, I am not able to do so properly. Following is my utility class:
public class FragmentUtil {
private FragmentUtil() {
}
public static void animatedReplace(FragmentActivity activity, int containerId, Fragment fragment, Bundle args, boolean addToBackStack){
FragmentTransaction transaction=activity.getSupportFragmentManager().beginTransaction();
fragment.setArguments(args);
if(addToBackStack){
transaction.addToBackStack(fragment.getClass().getName());
}
transaction.setCustomAnimations(R.anim.slide_from_right, R.anim.slide_to_left,R.anim.slide_from_left,R.anim.slide_to_right);
transaction.replace(containerId,fragment);
transaction.commit();
}
public static void clearBackStackToHome(FragmentActivity activity){
FragmentManager manager = activity.getSupportFragmentManager();
manager.popBackStack(DashboardFragment.class.getName(),FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
When I want to another fragment I just pass "true" for addToStack in animatedReplace method. When I want to clear the backstack I pass "false" and call the clearBackStackToHome method. Can anybody help me what I am doing wrong?