当前位置: 代码迷 >> Android >> Android Activity生命周期小结
  详细解决方案

Android Activity生命周期小结

热度:6   发布时间:2016-05-01 14:20:05.0
Android Activity生命周期总结

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

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

  1. Active/Runing一个新 Activity 启动入栈后,它在屏幕最前端,处于栈的最顶端,此时它处于可见并可和用户交互的激活状态。
  2. Paused 当 Activity 被另一个透明或者 Dialog 样式的 Activity 覆盖时的状态。此时它依然与窗口管理器保持连接,系统继续维护其内部状态,所以它仍然可见,但它已经失去了焦点故不可与用户交互。
  3. Stoped 当 Activity 被另外一个 Activity 覆盖、失去焦点并不可见时处于 Stoped状态。
  4. 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();

?

MethodDescriptionKillablenext
onCreate()当Activity第一次创建的时候被调用,在这个方法里你可以做一些初始化工作,创建视图,绑定数据等等,这个方法提供了Bundle参数,如果有先前的状态,你可以从这个参数中取得,他之后调用onStart()方法NOonStart()
onRestart()在调用onStop()方法之后会被调用,先前的activity又被调用了,下一个方法是onStart()NOonStart()
onStart()当Activity可见的时候被调用,当Activity从新回到前面下一个方法是onResume(),
onResume()
onPause()
onStop()
onDestroy()


?

Method Description Killable? NextonCreate()onRestart()onStart()onResume()onPause()onStop()onDestroy()
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().

NoonStart()
????Called after your activity has been stopped, prior to it being started again.

Always followed by onStart()

NoonStart()
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.

NoonResume() or onStop()
????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().

NoonPause()
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.

YesonResume() or
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.

YesonRestart() or
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.Yesnothing

?

?

?

?

?

?

?

  相关解决方案