Android开发笔记: Activity

Activity Stacks
  The state of each Activity is determined by its position on the Activity stack, a last-in–fi rst-out collection
of all the currently running Activities. When a new Activity starts, the current foreground screen is
moved to the top of the stack. If the user navigates back using the Back button, or the foreground Activity
is closed, the next Activity on the stack moves up and becomes active.This process is illustrated in
Figure 3-7.


  As described previously in this chapter, an application’s priority is infl uenced by its highest-priority
Activity. The Android memory manager uses this stack to determine the priority of applications based
on their Activities when deciding which application to terminate to free resources.

Activity States

  There are three nested loops that you can monitor by implementing them:

  • The entire lifetime of an activity happens between the first call to onCreate() through to a single final call to onDestroy() . An activity does all its initial setup of "global" state in onCreate() , and releases all remaining resources in onDestroy() . For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy() .
  • The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop() . During this time, the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods, you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user can no longer see what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity alternates between being visible and hidden to the user.

  • The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause() . During this time, the activity is in front of all other activities on screen and is interacting with the user. An activity can frequently transition between the resumed and paused states — for example, onPause() is called when the device goes to sleep or when a new activity is started, onResume() is called when an activity result or a new intent is delivered. Therefore, the code in these two methods should be fairly lightweight.

The following diagram illustrates these loops and the paths an activity may take between states. The colored ovals are major states the activity can be in. The square rectangles represent the callback methods you can implement to perform operations when the activity transitions between states.

 

Three methods (onPause() , onStop() , and onDestroy() ) are Killable. Because onPause() is the first of the three, it's the only one that's guaranteed to be called before the process is killed — onStop() and onDestroy() may not be. Therefore, you should use onPause() to write any persistent data (such as user edits) to storage.

 

Test Sample code:

 

The main Activity UI code of

ExampleActivity.java

 

The Second Activity UI code of

SecondActivity.java

 

Test Data:

 

When the program launch:

 

12-01 11:17:20.368: VERBOSE/Activity Lifecycle Main(353): onCreate
12-01 11:17:20.399: VERBOSE/Activity Lifecycle Main(353): onStart
12-01 11:17:20.410: VERBOSE/Activity Lifecycle Main(353): onResume

 

Then click the button to active the Second Activity:

 

ExampleActivity Log:


12-01 11:20:41.379: VERBOSE/Activity Lifecycle Main(353): onPause
12-01 11:20:41.829: VERBOSE/Activity Lifecycle Main(353): onStop

 

SecondActivity Log:

 

12-01 11:20:41.438: VERBOSE/Activity Lifecycle Second(353): onCreate
12-01 11:20:41.459: VERBOSE/Activity Lifecycle Second(353): onStart
12-01 11:20:41.459: VERBOSE/Activity Lifecycle Second(353): onResume

 

Close the Second Activity

 

ExampleActivity Log:

 

12-01 11:25:48.658: VERBOSE/Activity Lifecycle Main(353): onRestart
12-01 11:25:48.658: VERBOSE/Activity Lifecycle Main(353): onStart
12-01 11:25:48.658: VERBOSE/Activity Lifecycle Main(353): onResume

 

SecondActivity Log:

 

12-01 11:25:48.619: VERBOSE/Activity Lifecycle Second(353): onPause
12-01 11:25:48.958: VERBOSE/Activity Lifecycle Second(353): onStop
12-01 11:25:48.968: VERBOSE/Activity Lifecycle Second(353): onDestroy

 

小结:1. onCreate之后: 可以做onRestoreInstanceState,来恢复UI之前保存的数据.

         2. onPause之前: 可以做onSaveInstanceState,来保存UI当前的数据,以后将来恢复.

         3. 在某些极端情况下, 可能会不出现onDestory, onStop, onPause而程序就被killed掉了,这也就是为什么onSaveInstanceState在onPause之前调用.

         4. onPuase之后, 要过一些时间才会调用onStop. 所以onPause之后,可以马上调用onResume回来.

 

参考: Android 高级编程

        http://androidappdocs.appspot.com/guide/topics/fundamentals.html#acttask

 

Have a good day ;v)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值