使用哪一个:对的onSaveInstanceState onRetainNonConfigurationInstance?onSaveInstanceState、onRetainNonConfigur

2023-09-12 08:28:24 作者:饮惯烈酒

据我所看到onRetainNonConfigurationInstance是多余的回调。如果我的活动有真正昂贵的初始化,我最好使用的onSaveInstanceState。保存的实例包括比非配置实例的详细情况。是否有任何指引使用一个API与其他?谢谢你。

解决方案   

据我所看到onRetainNonConfigurationInstance是多余的回调。

没有,事实并非如此。

  

如果我的活动都有很贵的初始化,我最好使用的onSaveInstanceState。

的onSaveInstanceState()不是专为真贵初始化。它是专为哎,用户做了一些修改,以在该活动的信息,但还没有保存它,让我们不要失去这个数据,m'kay?

  

有没有准则使用一个API与其他?

中级不让带计算器,财管考试怎么办 今天我教你一招

如果它适合在捆绑并不算大,使用的onSaveInstanceState()。不适合在捆绑(例如,插座),或者是真正的大(例如,一张照片作为位图)应使用 onRetainNonConfigurationInstance(),和你的应用程序应该在的位置,如果需要重新创建这些项目。

As far as I can see onRetainNonConfigurationInstance is a redundant callback. If my activity has really expensive initialization, I am better off using onSaveInstanceState. Saved instance covers more situations than non-configuration instance. Is there any guideline for using one API vs. the other? Thanks.

解决方案

As far as I can see onRetainNonConfigurationInstance is a redundant callback.

No, it is not.

If my activity has really expensive initialization, I am better off using onSaveInstanceState.

onSaveInstanceState() is not designed for "really expensive initialization". It is designed for "hey, the user made some changes to the information in the activity but has not saved it yet, let's not lose that data, m'kay?".

Is there any guideline for using one API vs. the other?

If it fits in a Bundle and is not too big, use onSaveInstanceState(). Everything that does not fit in a Bundle (e.g., a socket) or is really big (e.g., a photo as a Bitmap) should use onRetainNonConfigurationInstance(), and your application should be in position to re-create those items if needed.