简体   繁体   English

设备轮换后,savedInstanceState 包始终为 null

[英]savedInstanceState bundle is always null after device rotation

(I know this question has been asked many times before, but none of the answers that I have read have helped.) (我知道这个问题以前被问过很多次,但我读过的答案都没有帮助。)

After I rotate my device, my fragment is recreated but the Bundle received in onCreate/onViewStateRestored/etc.旋转我的设备后,我的片段被重新创建,但在 onCreate/onViewStateRestored/etc 中收到了 Bundle。 is always null.始终为 null。 The fragment has a valid ID and retainInstance is set to false.片段有一个有效的 ID,retainInstance 设置为 false。

I am writing to the Bundle provided to the onSaveInstanceState method which is invoked as expected when the device is rotated.我正在写入提供给 onSaveInstanceState 方法的 Bundle,该方法在设备旋转时按预期调用。 After rotation, all the expected lifecycle callbacks are also invoked (onCreate, onActivityCreated, onViewStateRestored, etc.).在轮换之后,所有预期的生命周期回调也会被调用(onCreate、onActivityCreated、onViewStateRestored 等)。 However, the provided Bundle is null every case.但是,每个案例提供的 Bundle 都是 null。

Everything I have read on this topic would indicate that the fragment should receive the Bundle that was written to in onSaveInstanceState.我在这个主题上读到的所有内容都表明片段应该接收在 onSaveInstanceState 中写入的 Bundle。 The only thing I can think of that might be out of the ordinary is that the fragment is inside a navigation graph component.我唯一能想到的可能不寻常的是片段位于导航图组件内。 Note that I am not overriding onSaveInstanceState in the activity.请注意,我没有在活动中覆盖 onSaveInstanceState。

What could cause my fragment's Bundle to be null after rotation?什么可能导致我的片段的 Bundle 在旋转后成为 null?

It turns out that an override of onCreate in the Activity and was passing null instead of savedInstanceState to super.onCreate .事实证明,在 Activity 中覆盖onCreate并将 null 而不是 savedInstanceState 传递给super.onCreate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM