安卓的onSaveInstanceState()和的onPause() [英] Android: onSaveInstanceState() and onPause()

查看:106
本文介绍了安卓的onSaveInstanceState()和的onPause()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑在Android的这两种方法。

I'm a little bit confused about these two methods in Android.

这是我理解的文档的onSaveInstanceState()应该叫存储只是暂时的信息,而的onPause()应用于存储任何持久性数据。

From the documentation I understand that onSaveInstanceState() should be called to store only temporary information, and onPause() should be used to store any persistent data.

我想知道为什么要叫 onSaveInstance()可言,当的onPause()被称为每次。然后我读的记事本教程:

I was wondering why to call onSaveInstance() at all, when onPause() is called every time. Then I read in the Notepad tutorial :

需要注意的是 saveState和()必须是   所谓两个的onSaveInstanceState()   和的onPause(),以确保   数据被保存。这是因为有   不保证   的onSaveInstanceState()将被称为   而且,因为当它被调用时,它是   前的onPause()叫做

"Note that saveState() must be called in both onSaveInstanceState() and onPause() to ensure that the data is saved. This is because there is no guarantee that onSaveInstanceState() will be called and because when it is called, it is called before onPause()."

有没有保证的onSaveInstanceState()将被调用,因为你可以简单地走出使用后退按钮的活动。

There is no guarantee that onSaveInstanceState() will be called because you can simply walk out of the activity using the back button.

但是,根据这一点,如果你不救这两种方法里面的持久性数据,应用程序可能会死亡,而在的onSaveInstanceState()

But according to this if you don't save the persistent data inside both methods, the app might be killed while inside onSaveInstanceState().

因此​​,我们需要保存在这两种方法实际上是持久性数据,对吗?

So we need to save the persistent data in both methods actually, am I right ?

但是,如果这是真的,这不是太大的开销,也许应该有一些其他额外的标志来判断该方法已经叫什么?

But if this is true, isn't this too much of an overhead and maybe there should be some other additional flag to tell if the method is already called or something ?

<一个href="http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html">http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html

推荐答案

的onSaveInstanceState()是指当发生配置更改例如像记住当前的状态屏幕方向变化。这是不是意味着长期持续性。

onSaveInstanceState() is meant to "remember" the current state when a configuration change occurs like e.g. a screen orientation change. This is not meant for "long term persistence".

在记事本示例中,这可能是在这两种情况下是相同的。在其它应用中,例如像一个Twitter客户端,数据本身可能会坚持一个后台服务。在的onCreate()某些项目从数据库中拉出并显示和EG当前位置被记住。当方向发生变化时,这种当前位置可以在的onSaveInstanceState()记住后来在的onCreate()取向改变后用于显示该消息的用户已经最后看着

In the notepad example this may be the same in both cases. In other applications, like e.g. a Twitter client, the data itself may be persisted in a background service. In onCreate() some items are pulled from the DB and displayed and e.g. the current position is remembered. When an orientation change occurs, this "current position" could be remembered in onSaveInstanceState() and later in onCreate() after the orientation change be used to display the message the user has last looked at.

另请参阅本指南

这篇关于安卓的onSaveInstanceState()和的onPause()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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