popBackStack() after addToBackStack does not work

后端 未结 6 1349
梦毁少年i
梦毁少年i 2021-02-11 20:43

My project contains two fragment :

  • FragmentA : the fragment loaded by default when the app starts
  • FragmentB : replace the fragmentA when a c
6条回答
  •  孤街浪徒
    2021-02-11 20:57

    I will suggest using the fragment replacement with Tag to produce the same result .

    Add the fragment B to activity with tag

    fragmentTransaction.replace(R.id.main_fragment_container, new FragmentB(),"TAG_B");
    

    Fragment A -> Fragment B [onBackPressed] -->Fragment A Override the onBackPressed() in the Activity files where ,

    // check for fragment B and you are viewing fragment B

    if (getSupportFragmentManager().findFragmentByTag("TAG_B")!=null)  
    {
        fragmentTransaction.replace(R.id.main_fragment_container, new FragmentA(),"TAG_A");
    }
    

    addToBackStack("TAG") and popBackStackImmediate("TAG") always revert to fragment condition without any data in the UI right before fragment is created or added to activity !

提交回复
热议问题