您的位置:首页 > 移动开发 > Android开发

Android Activity生命周期总结

2010-10-22 10:50 483 查看
Android操作系统以一个stack的形式来管理activity,stack是一个后进先出的数据结构,当一个activity位于另一个activity之上并且是当前正在运行的activity,那么这个activity就到了这个stack的顶部。

正面是Activity的4种基本状态:

Active/Runing



一个新 Activity 启动入栈后,它在屏幕最前端,处于栈的最顶端,此时它处于可见并可和用户交互的激活状态。

Paused



当 Activity 被另一个透明或者 Dialog 样式的 Activity 覆盖时的状态。此时它依然与窗口管理器保持连接,系统继续维护其内部状态,所以它仍然可见,但它已经失去了焦点故不可与用户交互。

Stoped



当 Activity 被另外一个 Activity 覆盖、失去焦点并不可见时处于 Stop



ed



状态。

Killed



Activity 被系统杀死回收或者没有被启动时处于 Killed



状态。



在Android的生命周期中有6个函数来调用:

protected void onCreate(Bundle savedInstanceState);

protected void onStart();

protected void onResume();

protected void onPause();

protected void onStop();

protected void onDestroy();

Method

Description

Killable

next

onCreate()

当Activity第一次创建的时候被调用,在这个方法里你可以做一些初始化工作,创建视图,绑定数据等等,这个方法提供了Bundle参数,如果有先前的状态,你可以从这个参数中取得,他之后调用onStart()方法

NO

onStart()


onRestart()

在调用onStop()方法之后会被调用,先前的activity又被调用了,下一个方法是onStart()

NO

onStart()

onStart()

当Activity可见的时候被调用,当Activity从新回到前面下一个方法是onResume(),
onResume()

onPause()

onStop()

onDestroy()

MethodDescriptionKillable?Next
onCreate()

Called when the activity is first created.
This is where you should do all of your normal static set up:
create views, bind data to lists, etc. This method also
provides you with a Bundle containing the activity's previously
frozen state, if there was one.
Always followed by
onStart()


.

No
onStart()


onRestart()

Called after your activity has been stopped, prior to it being
started again.
Always followed by
onStart()


No
onStart()


onStart()

Called when the activity is becoming visible to the user.
Followed by
onResume()


if the activity comes
to the foreground, or
onStop()


if it becomes hidden.

No
onResume()


or
onStop()


onResume()

Called when the activity will start
interacting with the user. At this point your activity is at
the top of the activity stack, with user input going to it.
Always followed by
onPause()


.

No
onPause()


onPause()

Called when the system is about to start resuming a previous
activity. This is typically used to commit unsaved changes to
persistent data, stop animations and other things that may be consuming
CPU, etc. Implementations of this method must be very quick because
the next activity will not be resumed until this method returns.
Followed by either
onResume()


if the activity
returns back to the front, or
onStop()


if it becomes
invisible to the user.

Yes

onResume()


or

onStop()


onStop()

Called when the activity is no longer visible to the user, because
another activity has been resumed and is covering this one. This
may happen either because a new activity is being started, an existing
one is being brought in front of this one, or this one is being
destroyed.
Followed by either
onRestart()


if
this activity is coming back to interact with the user, or
onDestroy()


if this activity is going away.

Yes

onRestart()


or

onDestroy()


onDestroy()

The final call you receive before your
activity is destroyed. This can happen either because the
activity is finishing (someone called finish()

on
it, or because the system is temporarily destroying this
instance of the activity to save space. You can distinguish
between these two scenarios with the isFinishing()

method.
Yes

nothing
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: