活动的生命周期
返回栈
Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈就是返回栈。
当一个新的活动启动,他将放在返回栈的栈顶,并覆盖在原来的活动之上(原活动不一定被销毁,可能还在活动栈中),当点击Back销毁活动栈上最上面的活动。
活动的状态
运行状态:它位于返回栈的栈顶。
暂停状态:活动不在处于栈顶,但他依然可见!
停止状态:活动不处于栈顶,并且他完全不可见。
销毁状态:活动从返回栈中移除。
活动的生存周期
体验活动的生命周期
创建活动DialogActivity
package com.example.activitylifecycletest;
import android.app.Activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
}
创建活动NormalActivity
package com.example.activitylifecycletest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class NormalActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_normal);
}
}
编辑activity_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DialogActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a dialog activity"
tools:ignore="MissingConstraints"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
编辑activity_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NormalActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a normal activity"
tools:ignore="MissingConstraints"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
编辑activity_mian.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical">
<Button
android:id="@+id/start_normal_activity"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Start NormalActivity"
/>
<Button
android:id="@+id/start_dialog_activity"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Start DialogActivity"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
编辑AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitylifecycletest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">
<activity android:name=".NormalActivity">
</activity>
<activity android:name=".DialogActivity"
android:theme="@android:style/Theme.Dialog"
>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
编辑MainActivity
package com.example.activitylifecycletest;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.annotation.CallSuper;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
public static final String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startNormalActivity=(Button)findViewById(R.id.start_normal_activity);
Button startDailogActivity=(Button)findViewById(R.id.start_dialog_activity);
startNormalActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,NormalActivity.class);
startActivity(intent);
}
});
startDailogActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,DialogActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
@CallSuper
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart");
}
}
此时就可以查看打印信息
点击链接加入群聊【程序员技术交流群】:添加链接描述