阅读笔记3

来源:互联网 发布:五线谱打谱软件中文版 编辑:程序博客网 时间:2024/06/05 00:59
1.

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:

  1. Activity A's onPause() method executes.
  2. Activity B's onCreate()onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
  3. 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().

activity A 启动activity B,执行顺序如下:1.A onPause() 2.B onCreate --》 onStart -- 》 onResumn 3.A onStop方法,这样的生命周期的运行顺序意味着你可以管理你用来从一个activity到另一个activity过度的消息。例如,如果你想将数据存储到数据库中,开启的aicitivy能够使用,那你就应该在onPause方法中写入数据库,而不是放在onStop中
0 0
原创粉丝点击