Activity生命周期总结

看了很多别人的总结,自己也写了一个。

虽然比较简单,但是作为资料备在这里还是不错的~

先放老图一张:

Activity生命周期总结

 

附上测试代码:

package uni.activity;



import android.app.Activity;

import android.os.Bundle;

import android.util.Log;



public class ActivityDemoActivity extends Activity {

    /** Called when the activity is first created. */

	

	private static final String TAG = "demo";  

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Log.d("demo", "the Activity onCreate ");  

    }

    

    protected void onStart(){  

    	super.onStart(); 

    	Log.i(TAG, "The activity state---->onStart");  

    }  

    

    protected void onRestart(){ 

		super.onRestart(); 

		Log.i(TAG, "The activity state---->onRestart"); 

    } 



    protected void onResume(){ 

    	super.onResume(); 

    	Log.i(TAG, "The activity state---->onResume"); 

    } 



    protected void onPause(){  

    	super.onPause(); 

    	Log.i(TAG, "The activity state---->onPause"); 

    } 



    protected void onStop(){  

    	super.onStop(); 

    	Log.i(TAG, "The activity state---->onStop");

    } 



    protected void onDestroy(){  

    	super.onDestroy(); 

    	Log.i(TAG, "The activity state---->onDestroy");

    }  

}

 

代码比较简单,主要是看各种操作下,程序时如何变化的

  • 1. 程序启动时候

11-25 07:34:58.828: DEBUG/demo(1093): the Activity onCreate
11-25 07:34:58.859: INFO/demo(1093): The activity state---->onStart
11-25 07:34:58.859: INFO/demo(1093): The activity state---->onResume

 

  • 2. 已启动,点击返回按钮

11-25 07:38:00.929: INFO/demo(1093): The activity state---->onPause
11-25 07:38:01.099: INFO/demo(1093): The activity state---->onStop
11-25 07:38:01.099: INFO/demo(1093): The activity state---->onDestroy

 

  • 3. 已启动,home按钮/接听电话时

11-25 07:39:47.528: INFO/demo(1093): The activity state---->onPause
11-25 07:39:47.938: INFO/demo(1093): The activity state---->onStop

 

  • 4. 接上,从home界面再进入程序/挂断电话

11-25 07:42:27.629: INFO/demo(1093): The activity state---->onRestart
11-25 07:42:27.629: INFO/demo(1093): The activity state---->onStart
11-25 07:42:27.638: INFO/demo(1093): The activity state---->onResume

 

  • 5. 另:程序里有多个Activity情况。假定有两个Activity:A,B
  • 5.1 A进入B的情况:

A.onPause

B.onCreate

B.onStart

B.onResume

A.onStop

 

  • 5.2 在B里,按返回键

B.onPause

A.onReatart

A.onStart

A.onResume

B.onStop

B.onDestroy

你可能感兴趣的:(Activity)