2

I have a fragments, for example A -> B -> C. When I'm in the fragment C, and press the back button I need to get on a fragment A. That is, fragment B is not necessary to add to backStack. Here is the code which implements the transition between fragments.

FragmentManager fragmentManager = this.getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

Fragment fragment = fragmentManager.findFragmentByTag(tag);
fragment.getArguments().putAll(args);

transaction.replace(idContainer, fragment, tag);
transaction.addToBackStack(null);
transaction.commit();

At the opening of the fragment C, I don't use transaction.addToBackStack(null);

And now I have the following problem. From the fragment С I click the back button and enters the fragment A, but two layers of fragments are superimposed on each other.

Like this screenshot

Ahmad Arif
  • 53
  • 1
  • 8
Vlad
  • 149
  • 7

3 Answers3

1

Transition from A to B

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new BFragment(), "BFragment").addToBackStack("A_TO_B").commit();

Transition from B to C

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new CFragment(), "CFragment").addToBackStack("B_TO_C").commit();

Override Activity's onBackPressed() method.

@Override
public void onBackPressed();
if (getSupportFragmentManager().findFragmentByTag("CFragment") != null) {
    getSupportFragmentManager().popBackStack("A_TO_B",
    FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else {
       super.onBackPressed();
       }
}
0

You can use following code in Fragment_C To go back to Fragment_A on Back press button from fragment_C

 @Override
public void onResume() {

    super.onResume();

    getView().setFocusableInTouchMode(true);
    getView().requestFocus();
    getView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){


                Fragment_A fragment = null;
                fragment = new Fragment_A();

                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.container_body, fragment);
                fragmentTransaction.commit();


                return true;

            }

            return false;
        }
    });
}
Dhanveer thakur
  • 902
  • 1
  • 9
  • 17
-1

When you call Transaction.addToBackStack(null), your Fragment added to backstack, so you shouldn't call when you don't want to add your fragment to backstack.

What you should do

When you are replacing your Fragment-A with Fragment-B you should not call Transaction.addToBackStack(null), but when you replace Fragment-B with Fragment-C you should call Transaction.addToBackStack(null).

Doing this your Fragment-A and Fragment-C will be backstacked but Fragment-B will not, now when you press the back button you will have Fragment-A.

Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28