阅读笔记3
来源:互联网 发布:五线谱打谱软件中文版 编辑:程序博客网 时间:2024/06/05 00:59
Saving activity state
The introduction to Managing the Activity Lifecycle briefly mentions that when an activity is paused or stopped, the state of the activity is retained.
当activity的状态变为paused或者stopped的时候,其实activity的状态仍然是保留的。
you can ensure that important information about the activity state is preserved by implementing an additional callback method that allows you to save information about the state of your activity: onSaveInstanceState()
.
可以通过重写onSaveInstanceState()方法来保存你的activity的信息。
The system calls onSaveInstanceState()
before making the activity vulnerable to destruction. The system passes this method a Bundle
in which you can save state information about the activity as name-value pairs, using methods such as putString()
and putInt()
. Then, if the system kills your application process and the user navigates back to your activity, the system recreates the activity and passes the Bundle
to bothonCreate()
and onRestoreInstanceState()
. Using either of these methods, you can extract your saved state from the Bundle
and restore the activity state. If there is no state information to restore, then theBundle
passed to you is null (which is the case when the activity is created for the first time).
系统在你的activity收到攻击而销毁的时候会调用onSaveInstanceState方法。系统通过bundle来保存状态信息。(Bundle对象有putString,putInt等方法),当系统杀掉你的应用进程同时用户操作返回你的activity,系统重新创建actiivty并且把bandle对象传递给onCreate方法和onRestoreInstanceState防范。通过这两个方法,你可以取出你保存在bundle中的信息。如果没有状态信息,bundle就为空。
Note: There's no guarantee that onSaveInstanceState()
will be called before your activity is destroyed, because there are cases in which it won't be necessary to save the state (such as when the user leaves your activity using the Back button, because the user is explicitly closing the activity). If the system callsonSaveInstanceState()
, it does so before onStop()
and possibly before onPause()
.
并不能保证onSaveInstanceState方法在你的activity销毁之前调用,因为有很多因素使得兄台那个并没有必要来保存state。如果系统调用了该方法,一定是在onStop之前调用,有可能也在onPause之前调用
Note: Because onSaveInstanceState()
is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause()
to store persistent data (such as data that should be saved to a database) when the user leaves the activity.
用onSaveInstntceState来保留UI的状态,不要用来存储持久化数据。应该用onPause()方法来实现。
The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process and one is starting the other. Here's the order of operations that occur when Activity A starts Acivity B:
- Activity A's
onPause()
method executes. - Activity B's
onCreate()
,onStart()
, andonResume()
methods execute in sequence. (Activity B now has user focus.) - Then, if Activity A is no longer visible on screen, its
onStop()
method executes.
This predictable sequence of lifecycle callbacks allows you to manage the transition of information from one activity to another. For example, if you must write to a database when the first activity stops so that the following activity can read it, then you should write to the database during onPause()
instead of during onStop()
.
- tcpl 阅读笔记 3
- X264阅读笔记3
- 论文阅读笔记3
- 阅读笔记3
- 论文阅读笔记3
- DA9034驱动程序阅读笔记(3)
- x264代码阅读笔记3
- 加密解密3-阅读笔记
- vnc 代码阅读笔记3
- effective C++阅读笔记 3
- ActiveAndroid 源码阅读笔记 (3)
- flashsim源码阅读笔记3
- 操作系统概念阅读笔记3
- 阅读笔记
- 阅读笔记
- 阅读笔记
- 阅读笔记
- 阅读笔记
- spring mvc访问静态资源的三种方式
- <a>标签使用onclick(this)来传递参数
- Objective-C 概述
- PS move连接到pc
- 【算法】算法之路开始
- 阅读笔记3
- 数据结构面试题1.2.7-翻转句子中单词的顺序
- jsp的九大内置对象和四大作用域说明
- 自己动手之使用反射和泛型,动态读取XML创建类实例并赋值
- Javascript模拟实现类class
- 隐式Intent
- 死锁的产生及其避免
- Amusing Digits(2015网易游戏笔试题)
- 无数被转彻底理解树状数组