使用哪一个:对的onSaveInstanceState onRetainNonConfigurationInstance? [英] Which one to use: onSaveInstanceState vs. onRetainNonConfigurationInstance?

查看:122
本文介绍了使用哪一个:对的onSaveInstanceState onRetainNonConfigurationInstance?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所看到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.

这篇关于使用哪一个:对的onSaveInstanceState onRetainNonConfigurationInstance?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆