popBackStack() after addToBackStack does not work

后端 未结 6 1687
野趣味
野趣味 2021-02-11 20:29

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 21:02

    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 !

提交回复
热议问题