我在我的 Android 应用程序中使用带有 ViewPager2 的 FragmentStateAdapter,我注意到当我滑动到另一个项目时适配器正在分离并暂停我的片段,即使我已经覆盖了适配器的 onDetachedFromRecyclerView() 方法以什么都不做。
这是我的适配器的相关代码:
public class MyFragmentStateAdapter extends FragmentStateAdapter {
// constructor and other methods...
@Override
public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
// Do nothing to keep fragment's view hierarchy intact
}
}
尽管有这段代码,当我滑动到另一个项目时,我仍然看到 onPause() 在我的片段上被调用。我检查了可能导致片段暂停的其他生命周期事件,并且我还确保将 offscreenPageLimit 设置为其他值,例如 2,因为我有 2 个片段。我不确定还有什么可能导致这个问题。
关于可能发生什么的任何想法,或者我如何防止我的片段被暂停?谢谢!