Replacing a Fragment with itself does not show anything

后端 未结 4 718
猫巷女王i
猫巷女王i 2021-01-12 20:19

I\'m trying to decide and show a fragment in activity\'s onResume method, but in case a previously added fragment is chosen again, then the activity goes bl

4条回答
  •  抹茶落季
    2021-01-12 20:45

    Finally, I had to put a check before replacing fragments. In case, an (already added) fragment is requested for replace again, I had to check if its already added then ignore the replacement, else proceed. For example:

    @Override
    protected void onResume() {
        if (!fragA.isAdded()) {
            FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
            trans.replace(R.id.myLayout, fragA);
            trans.commit();
            //getSupportFragmentManager().executePendingTransactions();   //unnecessary
        }
    }
    

提交回复
热议问题