`
modabobo
  • 浏览: 508416 次
文章分类
社区版块
存档分类
最新评论

【Android翻译】关于Activity的onSaveInstanceState调用时机的说明

 
阅读更多
关于Activity的onSaveInstanceState调用时机的说明
Activity的生命周期里并没有提到onSaveInstanceState的触发,这个函数提供了为我们在某些情况下保存Activity信息的机会,但需要注意的是这个函数不是什么时候都会被调用的,官方文档解释的比较清楚,特此
翻译一下。
原文出处:android-sdk-windows-1.5_r3/docs/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

protected void onSaveInstanceState (Bundle outState)

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both). This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle).

在activity被杀掉之前调用保存每个实例的状态,以保证该状态可以在onCreate(Bundle)或者onRestoreInstanceState(Bundle) (传入的Bundle参数是由onSaveInstanceState封装好的)中恢复。这个方法在一个activity被杀死前调用,当该activity在将来某个时刻回来时可以恢复其先前状态。例如,如果activity B启用后位于activity A的前端,在某个时刻activity A因为系统回收资源的问题要被杀掉,A通过onSaveInstanceState将有机会保存其用户界面状态,使得将来用户返回到activity A时能通过onCreate(Bundle)或者onRestoreInstanceState(Bundle)恢复界面的状态。

Do not confuse this method with activity lifecycle callbacks such as onPause(), which is always called when an activity is being placed in the background or on its way to destruction, or onStop() which is called before destruction. One example of when onPause() and onStop() is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause() is called and not onSaveInstanceState(Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState(Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

不要将这个方法和activity生命周期回调如onPause()或onStop()搞混淆了,onPause()在activtiy被放置到背景或者自行销毁时总会被调用,onStop()在activity被销毁时被调用。一个会调用onPause()和onStop(),但不触发onSaveInstanceState的例子是当用户从activity B返回到activity A时:没有必要调用B的onSaveInstanceState(Bundle),此时的B实例永远不会被恢复,因此系统会避免调用它。一个调用onPause()但不调用onSaveInstanceState的例子是当activity B启动并处在activity A的前端:如果在B的整个生命周期里A的用户界面状态都没有被破坏的话,系统是不会调用activity A的onSaveInstanceState(Bundle)的。

The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself. If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().

默认的实现负责了大部分UI实例状态(的保存),采用的方式是调用UI层上每个拥有id的view的onSaveInstanceState() ,并且保存当前获得焦点的view的id(所有保存的状态信息都会在默认的onRestoreInstanceState(Bundle)实现中恢复)。如果你覆写这个方法来保存额外的没有被各个view保存的信息,你可能想要在默认实现过程中调用或者自己保存每个视图的所有状态。如果被调用,这个方法会在onStop()前被触发,但系统并不保证是否在onPause()之前或者之后触发。

分享到:
评论

相关推荐

    onSaveInstanceState调用的小例子

    通过这个例子描述onSaveInstanceState何时调用。非常简单的小例子。

    Android onSaveInstanceState和onRestoreInstanceState触发的时机

    主要介绍了Android onSaveInstanceState和onRestoreInstanceState触发的时机的相关资料,需要的朋友可以参考下

    Android代码-自动帮你完成 onSaveInstanceState 和 onRestoreInstanceState 的相关操作

    No boilerplate code like onSaveInstanceState or onRestoreInstanceState any more. Getting started Just add the @AutoRestore annotation to your fields that need to be saved and restored in Activities, ...

    Android中用onSaveInstanceState保存Fragment状态的方法

    在Fragment里面,利用onSaveInstanceState保存数据,并可在onActivityCreated里面恢复数据。 public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ... ...

    Android 中 onSaveInstanceState()使用方法详解

    Android 中 onSaveInstanceState()使用方法详解 覆盖onSaveInstanceState方法,并在onCreate中检测savedInstanceState和获取保存的值 @Override protected void onSaveInstanceState(Bundle outState) { outState...

    Activity 的生命周期 以及 横屏竖屏切换时 Activity 的状态变化

    Activity 的生命周期 以及 横屏竖屏切换时 Activity 的状态变化 例如: 切换到横屏 onSaveInstanceState onPause onStop onDestroy onCreate onStart onRestoreInstanceState onResume

    Android相机 解决三星bug版本

    如果 activity 的销毁如果无法避免 那么在activity销毁之前调用 onSaveInstanceState 保存图片的路径 当activity重新创建的时候 会将 onSaveInstanceState 保存的文件传递给onCreate()当中 在onCreate当中 检查照片...

    Android-教程精华-开发

    Android中,Activity是所有程序的根本,所有程序的流程都运行在Activity之 中,Activity具有自己的生命周期(由系统控制生命周期,程序无法改变,但可 以用onSaveInstanceState保存其状态)。 对于Activity,关键是其...

    onSaveInstanceState保存数据demo

    切换横竖屏,如何保存数据

    联想与百度android面试题集

    联想与百度android的面试题合集 一 Activity的生命周期 二 让Activity变成一个窗口:Activity属性设定 三 你后台的Activity被系统 回收怎么办:onSaveInstanceState 四 调用与被调用:我们的通信使者 - Intent

    Android 代码分析

    1 Android build system 4 1.1 Basic 4 1.2 Executable 6 ...30.3 你后台的Activity被系统回收怎么办:onSaveInstanceState 115 30.4 调用与被调用:我们的通信使者Intent 116 31 编译Native C的helloworld 118

    Android代码-svc

    Easy and intuitive pattern library for Android Why SVC? MVP and MVVM use Fragment or Activity as "VIew" So when we write code inside the Fragment or Activity, codes are getting mixed with "View" code...

    Android面试题

    Android面试之---Activity的详细讲解 这篇文章会涉及到以下几个...三 你后台的Activity被系统回收怎么办:onSaveInstanceState 四 调用与被调用:我们的通信使者 - Intent Android面试之---Android的虚拟机Dalvik 介绍

    Android屏幕旋转 处理Activity与AsyncTask的最佳解决方案

    发生这些变化,Android会重启Activity,这时就需要保存activity的状态及与activity相关的任务,以便恢复activity的状态。 为此,google提供了三种解决方案: 对于少量数据: 通过onSaveInstanceState(),保存有关...

    retainstate

    发生这些变化,Android会重启Activity,这时就需要保存activity的状态及与activity相关的任务,以便恢复activity的状态。 为此,google提供了三种解决方案: 对于少量数据: 通过onSaveInstanceState(),保存有关...

    Android Activity 横竖屏切换的生命周期

    onSaveInstanceState(Bundle outState) :Activity 即将销毁时保存数据 onRestoreInstanceState(Bundle savedInstanceState) : Activity 重建或者恢复时候取出数据 横竖屏切换生命周期 1、启动程序进入Activity界面...

    AndroidStudy:Android学习过程的一些总结

    3、优化DialogFragment在Activity onSaveInstanceState()之后调用导致的问题 更详细移步:https://www.jianshu.com/p/ea70ba200b38 发布日志 2018/12/25 Gradle理论与实践一:Gradle入门 ( ) Gradle理论与实践二:...

Global site tag (gtag.js) - Google Analytics