Android DEV1:Activity的生命周期

代码:

package com.msra.xiaobin;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Log.d("OnCreate", "onCreate Method is executed.");
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.d("onDestroy", "onDestroy Method is executed.");
    }
    @Override
    public void onPause()
    {
        super.onPause();
        Log.d("onPause", "onPause Method is executed.");
    }
    @Override
    public void onRestart()
    {
        super.onRestart();
        Log.d("onRestart", "onRestart Method is executed.");
    }
    @Override
    public void onResume()
    {
        super.onResume();
        Log.d("onResume", "onResume Method is executed.");
    }
    @Override
    public void onStart()
    {
        super.onStart();
        Log.d("onStart", "onStart Method is executed.");
    }
    @Override
    public void onStop()
    {
        super.onStop();
        Log.d("onStop", "onStop Method is executed.");
    }
}
 

输出:

将程序Deploy到Android phone上后,尝试切换到待机界面,再进入程序,再退出。
在DDMS(Dalvik Debug Monitor Service)透视图中LogCat窗格可以看到debug的输出信息:

03-14 16:07:55.650: DEBUG/OnCreate(1282): onCreate Method is executed.
03-14 16:07:55.650: DEBUG/onStart(1282): onStart Method is executed.
03-14 16:07:55.650: DEBUG/onResume(1282): onResume Method is executed.
03-14 16:07:55.660: DEBUG/onPause(1282): onPause Method is executed.
03-14 16:09:32.560: DEBUG/onResume(1282): onResume Method is executed.
03-14 16:09:38.100: DEBUG/onPause(1282): onPause Method is executed.
03-14 16:09:41.800: DEBUG/onResume(1282): onResume Method is executed.
03-14 16:10:05.290: DEBUG/onPause(1282): onPause Method is executed.
03-14 16:10:05.760: DEBUG/onStop(1282): onStop Method is executed.
03-14 16:11:06.670: DEBUG/onRestart(1282): onRestart Method is executed.
03-14 16:11:06.670: DEBUG/onStart(1282): onStart Method is executed.
03-14 16:11:06.670: DEBUG/onResume(1282): onResume Method is executed.
03-14 16:11:17.210: DEBUG/onPause(1282): onPause Method is executed.
03-14 16:11:17.690: DEBUG/onStop(1282): onStop Method is executed.
03-14 16:11:25.310: DEBUG/onRestart(1282): onRestart Method is executed.
03-14 16:11:25.310: DEBUG/onStart(1282): onStart Method is executed.
03-14 16:11:25.310: DEBUG/onResume(1282): onResume Method is executed.
03-14 16:11:28.280: DEBUG/onPause(1282): onPause Method is executed.
03-14 16:11:28.750: DEBUG/onStop(1282): onStop Method is executed.
03-14 16:11:28.760: DEBUG/onDestroy(1282): onDestroy Method is executed.


由此可见Android Activity的生命周期运行过程!

posted @ 2011-03-14 18:05  capedium  阅读(404)  评论(0)    收藏  举报