Android开发笔记: Activity
2010-12-01 20:03
591 查看
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.
![](D:/test.jpg)
![](http://hi.csdn.net/attachment/201012/1/0_1291205167s7hh.gif)
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
[/code]
through to a
single final call to
[/code]
.
An activity does all its initial setup of "global" state in
,
and releases all remaining resources in
. For example,
if it has a thread running in the background to download data from the network,
it may create that thread in
and then stop the thread in
.
The visible lifetime
of an activity happens between a call to
[/code]
until a
corresponding call to
[/code]
.
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
in
to monitor for changes that impact your UI, and unregister
it in
when the user can no longer see what you are displaying.
The
and
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
[/code]
until a
corresponding call to
[/code]
.
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,
is called when
the device goes to sleep or when a new activity is started,
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.
![](D:/activity_lifecycle.png)
Three methods (
,
, and
) are Killable.
Because
is the first of the three, it's the only one that's
guaranteed to be called before the process is killed —
and
may not be. Therefore, you
should use
to write any persistent data (such as user
edits) to storage.
![](http://hi.csdn.net/attachment/201012/1/0_12912051580y0H.gif)
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)
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.
![](D:/test.jpg)
![](http://hi.csdn.net/attachment/201012/1/0_1291205167s7hh.gif)
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()
[/code]
through to a
single final call to
onDestroy()
[/code]
.
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()
[/code]
until a
corresponding call to
onStop()
[/code]
.
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()
[/code]
until a
corresponding call to
onPause()
[/code]
.
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.
![](D:/activity_lifecycle.png)
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.
![](http://hi.csdn.net/attachment/201012/1/0_12912051580y0H.gif)
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)
相关文章推荐
- Android 开发学习笔记(二)—— Activity和Intent
- android应用开发笔记---Activity生命周期二
- Android开发学习笔记-5 Activity-4 Activity生命周期
- Android开发总结笔记 四大组件之Activity(上) 1-2-1
- Android开发学习笔记之将Activity变成半透明对话框
- Android开发艺术探索笔记(2)- Activity的启动模式
- Android开发自学笔记(六):声明权限和Activity
- Android开发艺术探索笔记一:Activity的启动模式
- Android开发学习笔记-3 Activity-2 Activity和Intent关系
- Android开发艺术探索阅读笔记之一:Activity的生命周期和启动模式
- 【Android开发学习笔记】【第二课】Activity学习
- Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例
- android应用开发笔记---Activity和Intent
- Android开发学习笔记(十一)ListView/ActivityManager/ArrayAdapter学习
- Android 开发艺术探究-阅读笔记一 Activity 篇
- Android开发笔记之主题背景的修改(设置Theme.NoTitleBar后Activity背景变成黑色)
- Android开发权威指南笔记1-简介&Activity
- Android开发笔记 Activity间的通讯
- Android 高级开发笔记 第一章 Activity的生命周期探索
- Android开发学习笔记:Activity的生命周期浅析