android-fragments Android Fragment isAdded在配置更改后返回false(屏幕旋转)

ffdz8vbo  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(276)

我有一个带有ViewPager2的Activity。ScreenSlidePagerAdapter正在ViewPager中创建我的4个片段。在滑动片段后,isAdded为每个片段返回true。在屏幕旋转配置更改后,isAdded为所有片段返回false,但我必须保持添加它们。是否有可能重新添加它们,或者我如何保持添加它们?
更新1:配置更改后,对于所有片段,isAdded仅为false,我在配置更改前浏览了这些片段。对于其他片段,isAdded在配置更改后也为true
适配器代码为:

private inner class ScreenSlidePagerAdapter(fa: FragmentActivity, data: Bundle) : FragmentStateAdapter(fa) {
        val fragmentBundle: Bundle = data

        override fun createFragment(position: Int): Fragment {
            when (position) {
                1 -> { 
                    sliderFragment2.arguments = fragmentBundle                                  
                    return sliderFragment2
                }
                2 -> {
                    sliderFragment3.arguments = fragmentBundle
                    return sliderFragment3
                }
                3 -> {
                    sliderFragment4.arguments = fragmentBundle
                    return sliderFragment4
                }
                else -> {
                    sliderFragment1.arguments = fragmentBundle
                    return sliderFragment1
                }
            }
        }

        override fun getItemCount(): Int {
            return NUM_PAGES
        }
    }
}
jvlzgdj9

jvlzgdj91#

public class TestActivity extends FragmentActivity{
     @Override
     public void onCreate(Bundle savedInstanceState){
            //Use null, no restore
            super.onCreate(null);
            ...
     }
}

相关问题