skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. Try this

    MainActivity.kt

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        loadFragment(FirstFragment())    // add this line
    }
    
    
    private fun loadFragment(fragment: Fragment) {
        val fragmentManager = supportFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.frameLayout, fragment)
        fragmentManager.popBackStack()
        fragmentTransaction.commit()
    }
    

    FirstFragment.kt

     binding.btnNext.setOnClickListener {
            val fragmentManager = fragmentManager
            val fragmentTransaction = fragmentManager?.beginTransaction()
            fragmentTransaction?.add(R.id.frameLayout, SecondFragment(), getString(R.string.fragment_tag_title_second_fragment))
            fragmentTransaction?.addToBackStack("Second")
            fragmentTransaction?.commitAllowingStateLoss()
        }
    

    SecondFragment.kt

    private fun onClick() {
        binding.apply {
            btnNext.setOnClickListener { loadFragment(ThirdFragment()) }
            btnPrevious.setOnClickListener {   activity?.onBackPressed() }
        }
    }
    
    private fun loadFragment(fragment: Fragment) {
        val fragmentTransaction = fragmentManager?.beginTransaction()
        fragmentTransaction?.add(R.id.frameLayout, fragment,getString(R.string.fragment_tag_title_third_fragment))
        fragmentTransaction?.addToBackStack("third")
        fragmentTransaction?.commitAllowingStateLoss()
    }
    

    ThirdFragment.kt

     private fun onClick() {
        binding.apply {
            btnPrevious.setOnClickListener {
                activity?.onBackPressed()
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search