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

public class ExampleActivity extends Activity { private static final String TAG = "Activity Lifecycle Main"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "onCreate"); setContentView(R.layout.main); Button btNext = (Button) findViewById(R.id.next_activity); btNext.setOnClickListener(new Button.OnClickListener() { public void onClick(View view) { startActivity(new Intent(ExampleActivity.this, SecondActivity.class)); } }); } @Override protected void onStart() { super.onStart(); Log.v(TAG, "onStart"); } @Override protected void onRestart() { super.onRestart(); Log.v(TAG, "onRestart"); } @Override protected void onResume() { super.onResume(); Log.v(TAG, "onResume"); } @Override protected void onPause() { super.onPause(); Log.v(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.v(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.v(TAG, "onDestroy"); } }

The Second Activity UI code of

SecondActivity.java

public class SecondActivity extends Activity { private static final String TAG = "Activity Lifecycle Second"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "onCreate"); setContentView(R.layout.second); Button btMain = (Button) findViewById(R.id.previous_activity); btMain.setOnClickListener(new Button.OnClickListener() { public void onClick(View view) { finish(); } }); } @Override protected void onStart() { super.onStart(); Log.v(TAG, "onStart"); } @Override protected void onRestart() { super.onRestart(); Log.v(TAG, "onRestart"); } @Override protected void onResume() { super.onResume(); Log.v(TAG, "onResume"); } @Override protected void onPause() { super.onPause(); Log.v(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.v(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.v(TAG, "onDestroy"); } }

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)

更多相关文章

  1. 安卓控件注入
  2. AndroidStudio调用摄像头
  3. Android左右滑动切换图片
  4. android JNI tips
  5. Android(安卓)App 调用自带浏览器
  6. retrofit2持久化保存cookie
  7. android 异步通信简单小计
  8. Android9 framework 按键音调用流程及自定义按键音(替换原生按键
  9. Android在Service服务中调用Activity活动的方法,实现UI界面更新操

随机推荐

  1. Android(安卓)中添加水平线和垂直线方法
  2. Android学习—Button的四种OnClick响应方
  3. ubuntu环境下载android系统源码代码
  4. 初学Android,使用Drawable资源之使用Shap
  5. ButterKnife的用法。针对android studio3
  6. 【Android(安卓)开发教程】选项菜单
  7. 两种android图片裁剪方式
  8. Android(安卓)ApiDemos示例解析(39):App->
  9. [android] ScrollView 设置滚动条的初始
  10. android(2) 功能引导界面实现