androidandroid-studioandroid-fragmentsfragment-backstack

3rd Fragment override 1st on backPress, when don't use addToBackStack in 2nd fragment


I open fragments like this: [1]+with addToBackStack) > [2](without addToBackStack) > [3]

in Fragment 3 when click onBackPress Button then 2nd is not visible but 3rd override 1. enter image description here

If I use addToBackStack in 2nd fragment also, then everything is fine(No override issue).

But I have to hide 2nd fragment when user visit 3rd fragment and direct come back to 1st fragment.

here is my code:

open root fragment[1] from activity with fragmentTransaction.add

FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
    fragmentTransaction.add(R.id.abbcHomeFragmentContainer, new FragmentHomeAbbc(), null);
    //fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

[1](with addToBackStack) >[2] :opening 2nd fragment

FragmentListOfBankAbbc fragment= new FragmentListOfBankAbbc();
    fragment.setArguments(bundle);
    if (getActivity()!=null){
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
        fragmentTransaction.replace(R.id.abbcHomeFragmentContainer, fragment, null);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    } else {
        Toast.makeText(getContext(), "Something went wrong (#201)", Toast.LENGTH_SHORT).show();
    }

[2](without addToBackStack) > [3]

private void  funOpenFragments(Fragment fragment, Bundle bundle){
    fragment.setArguments(bundle);
    if (getActivity()!=null){
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
        fragmentTransaction.replace(R.id.abbcHomeFragmentContainer, fragment, null);
        //fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

    } else {
        Toast.makeText(getContext(), "Something went wrong (#202)", Toast.LENGTH_SHORT).show();
    }

}

Now open 3rd fragment and click showing 3rd fragment+1st fragment.


Solution

  • When user go [1] > [2] > [3]

    Then onBackPress user will be [3] > [1].

    Finally, I got Solution.

    follow these 2 steps inside [2nd] fragment:

    Step1. Just add fragmentManager.popBackStack(); before fragmentTransaction mean calling [3] fragment. (after this line, It destroy/pop 2nd fragment and open 1st fragment again before open 3rd fragment. It mean when users press back button on 3rd fragment. then in recent fragment, there is 1st fragment not 2nd)
    Step2. add .addToBackStack(null) also with fragmentTransaction

    enter image description here

    [2] Fragment : here on button click we will open 3rd fragment .

    if (getActivity()!=null){
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    
        //this popBackStack will close this [2]fragment.
        fragmentManager.popBackStack(); //<<<<<<<just add this before "fragmentTransaction".
    
        //now open [3]fragment here, Now in recent fragment is only [1]fragment not [2].
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
        fragmentTransaction.replace(R.id.abbcHomeFragmentContainer, fragment, null);
        fragmentTransaction.addToBackStack(null); //<<<<<<<<<add this also
        fragmentTransaction.commit();
    
    } else {
        Toast.makeText(getContext(), "Something went wrong (#202)", Toast.LENGTH_SHORT).show();
    }
    

    (^_^) Happy Coding