4

I am using fragmentTransaction to proceed through fragments, it looks like this:

        FragmentManager fm = oldFragment.getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.add(oldFragment.getId(), newFragment, tag);
        fragmentTransaction.hide(oldFragment);
        fragmentTransaction.addToBackStack(tag);
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        fragmentTransaction.commit();

Having several fragments I want to skip some of them when going back.

Example:

Frag1 -> (Press Next) -> Frag2 -> (Press Next) -> Frag3 -> (Press Back) -> Frag1

This solution works when I use replace instead of add and hide

getFragmentManager().popBackStack(
    tag, 
    FragmentManager.POP_BACK_STACK_INCLUSIVE);

Reference` Skip some fragments onBackPressed

But in my case the skipped fragments are shown for a brief moment. How can I make it as if the fragments in between were never there?

Community
  • 1
  • 1
Belzebub
  • 944
  • 1
  • 9
  • 20

1 Answers1

0

I use this function to remove fragments tell me if I helped .

public static void removeFragment(FragmentActivity activity) {
    try {
        FragmentManager supportFragmentManager = activity.getSupportFragmentManager(); // getFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.slide_down, R.anim.slide_up);
        fragmentTransaction.remove(supportFragmentManager.getFragments().get(0));

        try {
            fragmentTransaction.commit();

        } catch (Exception e) {
        }
        supportFragmentManager.popBackStack();

    } catch (NullPointerException exception) {
        exception.printStackTrace();
    }
}
Francisco Castro
  • 435
  • 10
  • 20