代码实现的功能:程序运行后出现activity01的界面,界面上有一个按钮“开启activity02”,点击按钮,出现activity02界面
目的:验证activity生命周期为onCreat->onStart->onResume->onPause->onStop->onRestart->onStart.
Activity01的布局代码(activity01.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Activity01">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="click"(设置点击按钮时的响应函数)
android:text="开启activity02"/>
</RelativeLayout>
Activity02的布局代码(activity02.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Activity02">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="activity02"
android:textSize="30sp"/>
</RelativeLayout>
Activity01.java(创建activity01):
package com.wwj.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
/**
* Created by Administrator on 2017/3/30.
*/
public class Activity01 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {//创建activity01
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);//加载activity01的布局文件
Log.i("Activity01","onCreat()");//activity01创建时打印onCreat()
}
@Override
protected void onStart() {
super.onStart();
Log.i("activity01","onStart()");//activity01可见时打印onStart()
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("activity01","onRestart()");//activity01再次可见时打印onRestart()
}
@Override
protected void onResume() {
super.onResume();
Log.i("activity01","onResume()");//activity01获取焦点时打印onResume()
}
@Override
protected void onPause() {
super.onPause();
Log.i("activity01","onPause()");//activity01失去焦点时打印onPause()
}
@Override
protected void onStop() {
super.onStop();
Log.i("activity01","onStop()");//activity01不可见进入后台时打印onStop()
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("activity01","onDestroy()");//activity01销毁时打印onDestroy()
}
public void click(View view){//activity01界面上的按钮被点击时调用该函数
Intent intent = new Intent(this,Activity02.class);
startActivity(intent);//开启activity02界面
}
}
activity02.java(创建activity02):
package com.wwj.activity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
/**
* Created by Administrator on 2017/3/30.
*/
public class Activity02 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
Log.i("Activity02","onCreat()");
}
@Override
protected void onStart() {
super.onStart();
Log.i("activity02","onStart()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("activity02","onRestart()");
}
@Override
protected void onResume() {
super.onResume();
Log.i("activity02","onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.i("activity02","onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.i("activity02","onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("activity02","onDestroy()");
}
}
在AndroidManifest.xml中注册activity01和activity02:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wwj.activity" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"(设置app图标)
android:label="@string/app_name"(设置app名字)
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >(设置活动界面主题)
<activity android:name=".Activity01" android:label="@string/app_name">(注册activity01)
<intent-filter>
<action android:name="android.intent.action.MAIN"/>(app最先启动该活动)
<category android:name="android.intent.category.LAUNCHER"/>(app图标由该活动创建)
</intent-filter>
</activity>(注册activity02)
<activity android:name=".Activity02" android:label="我是activity02">
</activity>
</application>
</manifest>
(当activity具有label属性时,app名字、进入app后的界面标题由label的值决定)
运行结果:
点击按钮:
在activity02界面上点击返回退回到activity01界面:
由以上运行结果可知:当打开一个界面时,activity会先创建(onCreat),然后可见(start),获得焦点(onResume);由该界面开启另一界面时会先对让界面失去焦点并被保存,然后新界面被创建、可见、获得焦点,至此,原界面进入后台执行(onStop);由新界面退回原界面时会让新界面失去焦点并被保存,然后原界面重新可见(onRestart->onStart)、获得焦点,之后新界面进入后台(onStop)、被销毁(onDestroy)。
备注:学习参考《Android移动应用基础教程》。