一、准备工作
1. 新建一个project,创建三个Activity,分别叫MainActivity、NormalActivity、DialogActivity。
2. AndroidManifest.xml中,
<activity android:name=".DialogActivity" />
修改为
<activity android:name=".DialogActivity" android:theme="@style/Theme.AppCompat.Dialog"></activity>
这一步的目的是,使DialogActivity 成为对话框活动。
3. 其余代码放在文末,比较简单。
二、分析
(一) MainActivity -> NormalActivity
1. 运行,发现控制台输出了 onCreate,onStart,onResume,此时 活动处于运行状态。
2. 点击startNormalActivity按钮,控制台输出onPause,onStop。此时MainActivity不再处于栈顶,且不可见。
3. 返回,控制台输出,onRestart,onStart,onResume,此时MainActivity处于运行状态。
(二) MainActivity -> DialogActivity
1. 点击startDialogActivity按钮,控制台输出onPause。MainActivity不处于栈顶,但是可见。
2. 返回,控制台输出onResume,MainActivity重新回到运行状态。
附:
MainActivity.java
package com.androidstudy.activityrecycletest; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private Button btn_start_normal_activity,btn_start_dialog_activity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); Log.d(TAG, "onCreate: "); } @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: "); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart: "); } /** * 视图初始化 */ public void initView(){ btn_start_normal_activity=findViewById(R.id.btn_start_normal_activity); btn_start_dialog_activity=findViewById(R.id.btn_start_dialog_activity); //绑定 点击事件的监听器 btn_start_normal_activity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,NormalActivity.class); startActivity(intent); } }); btn_start_dialog_activity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,DialogActivity.class); startActivity(intent); } }); } }
Dialog.java
package com.androidstudy.activityrecycletest; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class DialogActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialog); } }
NormalActivity.java
package com.androidstudy.activityrecycletest; 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_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <Button android:id="@+id/btn_start_normal_activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="startNormalActivity" /> <Button android:id="@+id/btn_start_dialog_activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="startDialogActivity" /> </LinearLayout>
activity_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".DialogActivity"> <TextView android:text="this is a dialog activity" android:textSize="16sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </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:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".NormalActivity"> <TextView android:text="this is a normal activity" android:textSize="16sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NormalActivity">
<TextView
android:text="this is a normal activity"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>