Refresh fragment is not working any more?

前端 未结 4 1034
失恋的感觉
失恋的感觉 2021-01-05 07:23

I lost some hours today because my code was not working any more. The code to reload the view of a fragment was not working anymore after updating to the new version of Supp

4条回答
  •  花落未央
    2021-01-05 07:40

    @Viad actually answered the question. To add a little bit to it, this happens in android versions 26 and above where reordering is allowed by default. Reordering comes into play when two fragment operations are requested at the same done, for example adding fragment 1 and then replacing it with fragment 2, which causes only the latter (replacing fragment 2) to happen.

    So when reordering is allowed by default, when restarting the fragment using detach(fragment).attach(fragment) the first one is ignored and only second one is executed. As the fragment is already attached, attach(fragment) does not do anything. This is why none of the lifecycle methods of the fragment is called.

    The resolution to the problem would be to setReorderingAllowed(false) to deactivate reordering. So the solution would be:

                        FragmentTransaction transaction = mActivity.getFragmentManager()
                                .beginTransaction();
                        if (Build.VERSION.SDK_INT >= 26) {
                            transaction.setReorderingAllowed(false);
                        }
                        transaction.detach(fragment).attach
                                (fragment).commit();
    

提交回复
热议问题