如何刷新 Android Fragment v1.3.0 in Java

How to refresh Android Fragment v1.3.0 in Java

从新的 Fragment 版本 1.3.0 开始,在其内部刷新片段似乎不像在版本 1.2.5 中那样有效。

适用于我的项目 1.2.5 版本的代码:

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

但是在v1.3.0中使用这个无法刷新Fragment

有人能解决这个问题吗?我在发行说明中找不到详细说明任何可能成为问题的更改的文档。谢谢!

根据this issue

This is working as intended with the new state manager as mentioned in the Fragment 1.3.0-beta01 release notes as a requirement to fix an issue where exiting fragment views were not consistently removed before adding the entering one (aosp/1427376) which actually fixes a number of edge cases which can cause crashes.

You can change your code to do this recreation as two separate transactions:

fun Fragment.recreateView() {
    parentFragmentManager
        .beginTransaction()
        .detach(this)
        .commitNow()
    parentFragmentManager
        .beginTransaction()
        .attach(this)
        .commitNow()
}

接着说:

You might want to star b/173472486 for tracking a Lint warning to offer a quick fix exactly this pattern and b/165840276 for adding a first class API to fragments to recreate its view without needing detach()/attach().