Android系列教程之五:Activity的生命周期
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第一个Activity" /><Button android:id="@+id/second"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="打开第二个Activity"/></LinearLayout>?
这里主要是为增加一个文本显示和一个按钮用于显示信息和操作。新建布局文件second.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第二个Activity" /><Button android:id="@+id/exit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="退出"/></LinearLayout>?这里主要是为增加一个文本显示和一个退出按钮用于退出当前Activity。新建一个Activity,名字为SecondActivity,内容如下:
public class SecondActivity extends Activity {private final static String TAG="SecondActivity";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.v(TAG, "onCreate");setContentView(R.layout.second);//退出按钮Button btnExit=(Button)findViewById(R.id.exit);//为退出按钮设置单击事件btnExit.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {finish();//关闭当前Activity,也就是退出}});}@Overrideprotected void onStart() {super.onStart();Log.v(TAG, "onStart");}@Overrideprotected void onResume() {super.onResume();Log.v(TAG, "onResume");}@Overrideprotected void onPause() {super.onPause();Log.v(TAG, "onPause");}@Overrideprotected void onStop() {super.onStop();Log.v(TAG, "onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.v(TAG, "onDestroy");}}?我在各个周期方法了都加了日志信息,便于跟踪Activity的运行过程修改HelloWorld类,内容如下:public class HelloWorld extends Activity {private final static String TAG="HelloWorld"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "onCreate"); setContentView(R.layout.main); //打开第二个Activity的按钮 Button btnSecond=(Button)findViewById(R.id.second); //设置单击事件 btnSecond.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(HelloWorld.this,SecondActivity.class));finish();//关闭当前Activity}}); }@Overrideprotected void onStart() {super.onStart();Log.v(TAG, "onStart");}@Overrideprotected void onResume() {super.onResume();Log.v(TAG, "onResume");}@Overrideprotected void onPause() {super.onPause();Log.v(TAG, "onPause");}@Overrideprotected void onStop() {super.onStop();Log.v(TAG, "onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.v(TAG, "onDestroy");}}运行程序,分析结果,发现当程序启动的时候,日志信息为下图:

根据上面例子可见一个Activity在启动的时候会执行onCreate()->onStart()->onResume(),在结束(或离开)的时候会执行onPause()->onStop()->onDestroy(),这就是一个Activity的生命周期。
因此我们要在onCreate方法里把Activity的需要的东西准备好,也就是初始化;在onResume里对Activity里的东西做一些调整;在onPause做一些清理和保存工作(保存持久状态),因为这是最后的
机会,因为onPause完成之前Android不会结束托管Activity类的进程,而之后进程可能被结束。总结一下这几个周期方法的作用:
好了,最后让我们看一个API提供的Activity的状态图吧,看了他相信你对Activity的生命周期会更了解,如下图:
?
四:小结这节主要是通过一个例子分析Activity声明周期,并对常用生命周期方法做了一些说明,应该什么时候使用他们。到这里Android的很基础的东西已经说完了,
下面就直接Android的UI组件介绍了。。
下期预告:TextView的介绍--包含跑马灯效果
?