返回栈
-
Android是使用任务(task)来管理Activity的,一个任务就是一组存放在栈里的Activity集合,这个栈也被称作返回栈(back stack)。
-
栈是一种后进先出的数据结构,在默认情况下,每当我们启动了一个新的Activity,它就会在返回栈中入栈,并处于栈顶的位置。而每当我们按下Back键或调用finish()方法去销毁一个Activity时,处于栈顶的Activity就会出栈,前一个入栈的Activity就会重新处于栈顶的位置。
-
系统总是会显示处于栈顶的Activity给用户。
Activity的生命周期
-
onCreate() 这个方法在Activity第一次创建的时候调用。
-
onStart()这个方法在Activity由不可见变为可见的时候调用。
-
onResume()这个方法在Activity准备好和用户进行交互的时候调用。
-
onPause()这个方法在系统准备去启动或者恢复的另一个Activity的时候调用。
-
onStop()这个方法在Activity完全不可见的时候调用。
-
onDestroy()这个方法在Activity被销毁之前调用。
-
onRestart()这个方法在Activity由停止状态变为运行状态之前调用,也就是Activity被重新启动了。
Activity的生命周期示意图
测试:
创建三个Activity,其中DialogActivity设置主体为Dialog
类型:
代码如下:
MainActivity
package com.easycol.reactivitylifecycle
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private val tag = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d(tag, "onCreate")
startNormalActivity.setOnClickListener{
val intent = Intent(this, NormalActivity::class.java)
startActivity(intent)
}
startDialogActivity.setOnClickListener{
val intent = Intent(this, DialogActivity::class.java)
startActivity(intent)
}
}
override fun onStart() {
super.onStart()
Log.d(tag, "onStart")
}
override fun onResume() {
super.onResume()
Log.d(tag, "onResume")
}
override fun onPause() {
super.onPause()
Log.d(tag, "onPause")
}
override fun onStop() {
super.onStop()
Log.d(tag, "onStop")
}
override fun onDestroy() {
super.onDestroy()
Log.d(tag, "onDestroy")
}
override fun onRestart() {
super.onRestart()
Log.d(tag, "onRestart")
}
}
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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:textSize="18dp"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark"
android:layout_gravity="center_horizontal"
android:text="Actvity的生命周期"/>
<Button
android:id="@+id/startNormalActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:text="Start NormalActivity"/>
<Button
android:id="@+id/startDialogActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Start DialogActivity"/>
</LinearLayout>
DialogActivity
package com.easycol.reactivitylifecycle
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class DialogActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dialog)
}
}
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
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="This is a dialog activity"
android:textAlignment="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
NormalActivity
package com.easycol.reactivitylifecycle
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class NormalActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_normal)
}
}
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
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="This is a normal activity"
android:textAlignment="center"
android:gravity="center"
android:textSize="18dp"
android:textColor="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</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.easycol.reactivitylifecycle">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".NormalActivity"></activity>
<activity android:name=".DialogActivity"
android:theme="@style/Theme.AppCompat.Dialog"/>
<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>
设置打印匹配:
- 启动程序:
1、程序默认启动的情况:
2020-10-01 16:05:37.338 25141-25141/com.easycol.reactivitylifecycle D/MainActivity: onCreate
2020-10-01 16:05:37.347 25141-25141/com.easycol.reactivitylifecycle D/MainActivity: onStart
2020-10-01 16:05:37.349 25141-25141/com.easycol.reactivitylifecycle D/MainActivity: onResume
2、点击跳转到另一个Activity(NormalActivity
),因为此时已经跳转到另一个Activity,打印结果如下:
2020-10-01 16:11:40.891 25141-25141/com.easycol.reactivitylifecycle D/MainActivity: onPause
2020-10-01 16:11:41.671 25141-25141/com.easycol.reactivitylifecycle D/MainActivity: onStop
3、从NormalActivity
返回到MainActivity
2020-10-01 16:37:56.449 25141-25141/com.easycol.reactivitylifecycle D/MainActivity: onRestart
2020-10-01 16:37:56.451 25141-25141/com.easycol.reactivitylifecycle D/MainActivity: onStart
2020-10-01 16:37:56.457 25141-25141/com.easycol.reactivitylifecycle D/MainActivity: onResume
4、点击弹出DialogActivity
:
2020-10-01 16:39:25.314 25141-25141/com.easycol.reactivitylifecycle D/MainActivity: onPause
5、让DialogActivity
消失:
2020-10-01 16:40:09.502 25141-25141/com.easycol.reactivitylifecycle D/MainActivity: onResume
- 在
NormalActivity
中也加上打印代码:
package com.easycol.reactivitylifecycle
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class NormalActivity : AppCompatActivity() {
private val tag = "NormalActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_normal)
Log.d(tag, "onCreate")
}
override fun onStart() {
super.onStart()
Log.d(tag, "onStart")
}
override fun onResume() {
super.onResume()
Log.d(tag, "onResume")
}
override fun onPause() {
super.onPause()
Log.d(tag, "onPause")
}
override fun onStop() {
super.onStop()
Log.d(tag, "onStop")
}
override fun onDestroy() {
super.onDestroy()
Log.d(tag, "onDestroy")
}
override fun onRestart() {
super.onRestart()
Log.d(tag, "onRestart")
}
}
1、进入NormalActivity
时:
2020-10-01 16:41:56.229 25141-25141/com.easycol.reactivitylifecycle D/NormalActivity: onCreate
2020-10-01 16:41:56.235 25141-25141/com.easycol.reactivitylifecycle D/NormalActivity: onStart
2020-10-01 16:41:56.237 25141-25141/com.easycol.reactivitylifecycle D/NormalActivity: onResume
2、从NormalActivity
返回到MainActivity
,因为NormalActivity
已经出栈了,所以会调用onDestroy
:
2020-10-01 16:43:39.681 25141-25141/com.easycol.reactivitylifecycle D/NormalActivity: onPause
2020-10-01 16:43:40.342 25141-25141/com.easycol.reactivitylifecycle D/NormalActivity: onStop
2020-10-01 16:43:40.344 25141-25141/com.easycol.reactivitylifecycle D/NormalActivity: onDestroy