Saving activity state--保存activity实例的状态信息
When the system, rather than the user, shuts down an activity to conserve memory, the user may expect to return to the activity and find it in its previous state.【翻译:系统需要更多内存资源的时候会销毁activity实例,用户希望系统能够在实例重新回到前台的时候能恢复之前的状态信息。】
To capture that state before the activity is killed, you can implement an onSaveInstanceState() method for the activity. Android calls this method before making the activity vulnerable to being destroyed — that is, before onPause() is called. It passes the method a Bundle object where you can record the dynamic state of the activity as name-value pairs. When the activity is again started, the Bundle is passed both to onCreate() and to a method that's called after onStart(), onRestoreInstanceState(), so that either or both of them can recreate the captured state.
翻译:在activity被销毁之前要想俘获实例的状态信息,开发者需要在activity中实现onSaveInstanceState()方法,Android系统会在被销毁之前调用该方法,准确说是在 onPause()方法被调用之前,系统会调用onSaveInstanceState()方法。这个方法的参数是一个Bundle对象,这个参数中以name-value的形式持有这个activity实例的属性状态信息。当这个activity类的另外一个实例被创建的时候,这个Bundle对象会作为参数传递给onCreate() 、onStart() 和onRestoreInstanceState(), 方法。所以在这三个方法中,都可以恢复activity实例之前的状态信息。
Unlike onPause() and the other methods discussed earlier, onSaveInstanceState() and onRestoreInstanceState() are not lifecycle methods. They are not always called. For example, Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key). In that case, the user won't expect to return to the activity, so there's no reason to save its state.
翻译:onSaveInstanceState() 和 onRestoreInstanceState()方法与我们之前讨论过的其他生命周期回调方法不同,这两个方法不是生命周期方法,这两个方法不总是被系统调用,例如:Android系统会在activity实例被销毁前调用onSaveInstanceState()方法,系统不会在按下BACK键的时候(实例弹出当前task堆栈,这时候实例也被销毁)多余地去调用onSaveInstanceState()方法。因为这时候用户其实并不期望再次返回到该实例的界面,所以不需要保存该实例的状态信息。
Because onSaveInstanceState() is not always called, you should use it only to record the transient state of the activity, not to store persistent data. Use onPause() for that purpose instead.
正是因为onSaveInstanceState() 不是经常被系统调用,所以,开发者实现该方法通常是为了保留activity实例的临时状态信息,如果是需要保存activity实例的持久化信息,应该是在onPause()方法中实现。
When the system, rather than the user, shuts down an activity to conserve memory, the user may expect to return to the activity and find it in its previous state.【翻译:系统需要更多内存资源的时候会销毁activity实例,用户希望系统能够在实例重新回到前台的时候能恢复之前的状态信息。】
To capture that state before the activity is killed, you can implement an onSaveInstanceState() method for the activity. Android calls this method before making the activity vulnerable to being destroyed — that is, before onPause() is called. It passes the method a Bundle object where you can record the dynamic state of the activity as name-value pairs. When the activity is again started, the Bundle is passed both to onCreate() and to a method that's called after onStart(), onRestoreInstanceState(), so that either or both of them can recreate the captured state.
翻译:在activity被销毁之前要想俘获实例的状态信息,开发者需要在activity中实现onSaveInstanceState()方法,Android系统会在被销毁之前调用该方法,准确说是在 onPause()方法被调用之前,系统会调用onSaveInstanceState()方法。这个方法的参数是一个Bundle对象,这个参数中以name-value的形式持有这个activity实例的属性状态信息。当这个activity类的另外一个实例被创建的时候,这个Bundle对象会作为参数传递给onCreate() 、onStart() 和onRestoreInstanceState(), 方法。所以在这三个方法中,都可以恢复activity实例之前的状态信息。
Unlike onPause() and the other methods discussed earlier, onSaveInstanceState() and onRestoreInstanceState() are not lifecycle methods. They are not always called. For example, Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key). In that case, the user won't expect to return to the activity, so there's no reason to save its state.
翻译:onSaveInstanceState() 和 onRestoreInstanceState()方法与我们之前讨论过的其他生命周期回调方法不同,这两个方法不是生命周期方法,这两个方法不总是被系统调用,例如:Android系统会在activity实例被销毁前调用onSaveInstanceState()方法,系统不会在按下BACK键的时候(实例弹出当前task堆栈,这时候实例也被销毁)多余地去调用onSaveInstanceState()方法。因为这时候用户其实并不期望再次返回到该实例的界面,所以不需要保存该实例的状态信息。
Because onSaveInstanceState() is not always called, you should use it only to record the transient state of the activity, not to store persistent data. Use onPause() for that purpose instead.
正是因为onSaveInstanceState() 不是经常被系统调用,所以,开发者实现该方法通常是为了保留activity实例的临时状态信息,如果是需要保存activity实例的持久化信息,应该是在onPause()方法中实现。
发表评论
-
Intents and Intent Filters--Intent filters
2010-09-03 10:21 1364Intent filters To inform ... -
Intents and Intent Filters---Intent Resolution
2010-09-03 09:45 1197Intent Resolution Intents 可以分成 ... -
Intents and Intent Filters---Intent Objects
2010-09-02 11:15 818Intent Objects An Intent ... -
Intents and Intent Filters---序言
2010-09-01 16:06 874Intents and Intent Filters Key ... -
Application Fundamentals--Processes and lifecycles(进程生命周期)
2010-08-30 09:23 754Processes and lifecycles---进 ... -
Application Fundamentals--Broadcast receiver lifecycle(广播接收组件生命周期)
2010-08-30 09:20 4243Broadcast receiver lifecycle--- ... -
Application Fundamentals--Service lifecycle(服务组件的生命周期)
2010-08-30 09:18 1051Service lifecycle--服务组件的生命周期 A ... -
Application Fundamentals--Coordinating activities(activity实例间协调)
2010-08-30 09:15 966Coordinating activities---activ ... -
Application Fundamentals--Component Lifecycles(组件生命周期)
2010-08-27 09:56 961Component Lifecycles--组件生命周期 A ... -
Application Fundamentals--Thread-safe methods(线程安全方法)
2010-08-27 09:51 1254Thread-safe methods--线程安 ... -
Application Fundamentals--Remote procedure calls(远程过程调用)
2010-08-27 09:41 1291Remote procedure calls--远程过程调用 ... -
Application Fundamentals--Processes and Threads(进程与线程)
2010-08-27 09:36 990Processes and Threads--进程 ... -
Application Fundamentals--Starting tasks
2010-08-27 09:34 701Starting tasks An activity is ... -
Application Fundamentals--Clearing the stack(堆栈清理)
2010-08-27 09:28 1345Clearing the stack--堆栈清理 If th ... -
Application Fundamentals--Launch modes-启动模式
2010-08-27 09:27 1118Launch modes-启动模式 There are fo ... -
Application Fundamentals--Affinities and new tasks
2010-08-27 08:45 979Affinities and new tasks By de ... -
Application Fundamentals--Activities and Tasks
2010-08-27 08:42 789Activities and Tasks As noted ... -
Application Fundamentals--Intent 过滤器
2010-08-27 08:41 921Intent 过滤器 Intent 对象可以显式指定目标组件 ... -
Application Fundamentals--The manifest file
2010-08-27 08:40 788The manifest file Android启动应用程 ... -
Application Fundamentals--Activating components: intents(组件的激活)
2010-08-27 08:38 934Activating components: intents- ...
相关推荐
DB2 9 Database and Application Fundamentals - Academic Initiative 000-302 2011年12月版本 V8.02 Q188道。
fundamentals-of-power-electronics_2nd_erickson_full
Fundamentals-of-HVAC-Control-Systems-Malestrom.part02.rar
linux-shell-scripting-fundamentals-bash.epub
Fundamentals-of-heat-and-mass-transfer 7ed.pdf ;INSTRUCTOR'S SOLUTIONS MANUAL
fundamentals-of-radar-signal
ios-12-programming-fundamentals-swift epub. very good
fundamentals-of-statistical-signal-processing-volume-i-estimation-theory_1
2016-fuel cell fundamentals-3rd edition.pdf
此存储库包含
Az-900-Microsoft-Azure-Fundamentals-Version-16.0-It-Exam-Practice-Test.pdf
Az-900-Microsoft-Azure-Fundamentals-Version-19.1-It-Exam-Practice-Test.pdf
07.通过部署管理状态(managing state with deployments) 08.卷和数据(Volumes and Data) 09.服务(services) 10. INGRESS 11.Scheduling 12.记录和故障排除(Logging and Troubleshooting) 13.Custom Resource Definit
Data Warehousing Fundamentals-2001
信息与通信工程专业毕业设计说明外文翻译--Fundamentals-of-Time-and-Frequency.doc
Electric Machinery Fundamentals - 5th Ed
ruby-oo-fundamentals-attribute-accessors-lab-wdc01-seng-ft-060120-源码.rar
ruby-oo-fundamentals-attribute-accessors-lab-sea01-seng-ft-071320-master.rar