1. Activity生命周期
1.1 知识点
(1)掌握Activity的生命周期及操作方法;
1.2 具体内容
范例:
第一个配置文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".ActivityLifeActivity" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这是第一个Activity" /><Button android:id="@+id/but_first"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="跳转到第二个Activity"/></LinearLayout>
第二个配置文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".ActivityLifeActivity" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这是第二个Activity" /><Button android:id="@+id/but_second"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="跳转到第三个Activity"/></LinearLayout>
第三个配置文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".ActivityLifeActivity" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这是第二个Activity" /></LinearLayout>
第一个Activity
package com.example.activitylifeproject;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class ActivityLifeActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_activity_life_first);Log.i("sign","第一个Activity的onCreate方法");Button but = (Button) super.findViewById(R.id.but_first);but.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent it = new Intent(ActivityLifeActivity.this,ActivityLifeSecondActivity.class);startActivity(it);}});}@Overrideprotected void onStart(){Log.i("sign","第一个Activity的onStart方法");super.onStart();}@Overrideprotected void onResume(){Log.i("sign","第一个Activity的onResume方法");super.onResume();}@Overrideprotected void onPause(){Log.i("sign","第一个Activity的onPause方法");super.onPause();}@Overrideprotected void onStop(){Log.i("sign","第一个Activity的onStop方法");super.onStop();}@Overrideprotected void onDestroy(){Log.i("sign","第一个Activity的onDestroy方法");super.onDestroy();}@Overrideprotected void onRestart(){Log.i("sign","第一个Activity的onRestart方法");super.onRestart();}
}
第二个Activity
package com.example.activitylifeproject;import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class ActivityLifeSecondActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_activity_life_second);Log.i("sign","第二个Activity的onCreate方法");Button but = (Button) super.findViewById(R.id.but_second);but.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent it = new Intent(ActivityLifeSecondActivity.this,ActivityLifeThirdActivity.class);startActivity(it);}});}@Overrideprotected void onStart(){Log.i("sign","第二个Activity的onStart方法");super.onStart();}@Overrideprotected void onResume(){Log.i("sign","第二个Activity的onResume方法");super.onResume();}@Overrideprotected void onPause(){Log.i("sign","第二个Activity的onPause方法");super.onPause();}@Overrideprotected void onStop(){Log.i("sign","第二个Activity的onStop方法");super.onStop();}@Overrideprotected void onDestroy(){Log.i("sign","第二个Activity的onDestroy方法");super.onDestroy();}@Overrideprotected void onRestart(){Log.i("sign","第二个Activity的onRestart方法");super.onRestart();}
}
第三个Activity
package com.example.activitylifeproject;import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;public class ActivityLifeThirdActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_activity_life_third);}@Overrideprotected void onStart(){Log.i("sign","第三个Activity的onStart方法");super.onStart();}@Overrideprotected void onResume(){Log.i("sign","第三个Activity的onResume方法");super.onResume();}@Overrideprotected void onPause(){Log.i("sign","第三个Activity的onPause方法");super.onPause();}@Overrideprotected void onStop(){Log.i("sign","第三个Activity的onStop方法");super.onStop();}@Overrideprotected void onDestroy(){Log.i("sign","第三个Activity的onDestroy方法");super.onDestroy();}@Overrideprotected void onRestart(){Log.i("sign","第三个Activity的onRestart方法");super.onRestart();}}
1.3 小结
(1)Activity提供了专门的生命周期方法;
(2)在多个Activity打开的时候,会自动的进行入栈操作;