Android生命周期
[Android]
#1 Activity:
Activity是一个交互界面,可以布满整个窗口,或者悬浮于其他窗口上
一个程序由多个Activity组成,Manifest中指明了主Activity
<activity <span style="white-space:pre"> </span>android:label="@string/app_name" <span style="white-space:pre"> </span>android:name=".MainActivity" > <intent-filter > <span style="white-space:pre"> </span><action android:name="android.intent.action.MAIN" /> <span style="white-space:pre"> </span><category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Activity是一个互相调用关系,以栈的形式存取
#2 生命周期
voidonCreate(BundlesavedInstanceState)
voidonStart()
voidonRestart()
voidonResume()
voidonPause()
voidonStop()
voidonDestroy()
开始Activity:onCreate、onStart和onResume方法。
Activity失去焦点:onPause、onStop
Activity重新获得焦点:onRestart、onStart和onResume
关闭Activity:onPause、onStop和onDestory
#3 测试代码
package com.android.mylife; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class Main extends Activity { /** Called when the activity is first created. */ private final String TAG = "Main"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.i(TAG, "onCreate Method is executed"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i(TAG, "onStart Method is executed"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.i(TAG, "onRestart Method is executed"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.i(TAG, "onResume Method is executed"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.i(TAG, "onStop Method is executed"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.i(TAG, "onPause Method is executed"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i(TAG, "onDestroy Method is executed"); } }