在 Android Oreo 8.0 中 onPause 之前调用的新 Fragment 的 onResume

onResume of new Fragment called before onPause in Android Oreo 8.0

我在 android 奥利奥方面遇到了麻烦。我的 MainActivity 有 4 个片段,只要用户按下选项卡,它们就会相互替换。现在的问题是,我在 onPause 的单例实例中保存了一个值。每当用户按下下一个选项卡时,该片段的 onResume 在 onPause 之前被调用,因此我无法从单例中正确检索值。

onResume 应该在 onPause 之前调用。 如果你想在启动 fragment 之前保存一些东西,请在 onStart 中进行。如果您想在离开片段之前保存一些东西,请在 onStop 中进行。 More info on Fragment lifecycle

oreo 中的片段存在一些问题 version.I 创建了一个单独的应用程序来检查当我们用另一个片段替换一个片段时调用了哪些生命周期函数。这是日志:

MainActivity onCreate ->
FragmentA oncreate ->
FragmentA oncreateView ->
MainActivity onStart ->
FragmentA onStart ->
MainActivity onResume ->
FragmentA onResume ->
Button Pressed;that replace fragment A with Fragment B
FragmentB onCreate ->
FragmentB onCreateView ->
FragmentB onStart ->
FragmentB onResume ->
FragmentA onPause ->
FragmentA onStop ->
FragmentA onDestroy ->

当我用 android.support.v4.app.Fragment .

替换它时,我正在导入 import android.app.Fragment.It 工作

请将 setReorderingAllowed 设置为 false 以获得正常的片段生命周期。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
fragmentTransaction.setReorderingAllowed(false);
} 

https://developer.android.com/reference/android/app/FragmentTransaction.html#setReorderingAllowed(boolean)