Dialog 究竟会不会触发 onPause() 生命周期函数?

谣言:

Dialog 会触发 onPause() 算是 Android 开发领域流传最广的谣言之一了。

Demo 验证:

建议大家自己动手操作一下。

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        open_dialog.setOnClickListener {
            AlertDialog.Builder(this)
                    .setMessage("普通 dialog 弹出")
                    .setCancelable(true)
                    .setPositiveButton("OK") { dialog, which ->

                    }
                    .create()
                    .show()
        }

        open_transparent_activity.setOnClickListener {
        	// 打开全屏透明 Activity
            val intent: Intent = Intent(this, CameraActivity::class.java)
            startActivity(intent)
        }

        open_system_dialog.setOnClickListener {
            // 吊起系统级别的 dialog,请求权限
            val hasWriteContactsPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)

            if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 0)
            } else {

            }
        }

    }

    override fun onPause() {
        super.onPause()
        Log.e("zhangrr", "MainActivity.onPause() called. ")
        val toast = Toast.makeText(this, "MainActivity.onPause() called.", Toast.LENGTH_SHORT)
        toast.setGravity(Gravity.TOP, 100, 500)
        toast.show()
    }

    override fun onStop() {
        super.onStop()
        Log.e("zhangrr", "MainActivity.onStop() called. ")
        val toast = Toast.makeText(this, "MainActivity.onStop() called.", Toast.LENGTH_SHORT)
        toast.setGravity(Gravity.TOP, 100, 500)
        toast.show()
    }
}

CameraActivity.kt

class CameraActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) // 显示状态栏
        setContentView(R.layout.activity_camera)
        window.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <Button
            android:id="@+id/open_transparent_activity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转全屏透明 Activity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <Button
            android:id="@+id/open_dialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打开普通 Dialog"
            android:layout_marginTop="30dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/open_transparent_activity"/>

    <Button
            android:id="@+id/open_system_dialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打开请求系统权限 Dialog"
            android:layout_marginTop="30dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/open_dialog"/>

</android.support.constraint.ConstraintLayout>

下面分别对三种情况进行测试,

1、普通的 Dialog

可以看到普通 Dialog 并没有调用 onPause()!!!!
在这里插入图片描述

2、系统级别的 Dialog

可以看到系统级别的权限申请 dialog 的弹出,调用了 onPause()!!!!
在这里插入图片描述

3、透明全屏 Activity

可以看到调用了 onPause()!!!!
在这里插入图片描述

原理分析:

先来看 sdk doc 中对 onPause() 的说明:

  1. onPause:Called when the system is about to start resuming another activity.

  2. onPause() is called when the device goes to sleep or when a dialog appears.

当然网上也有很多说法,比如 dialog 出现为何不会触发 onPause() ,依据就是第一条,说只有另一个 activity 的出现才会触发 onPause()。但再看看上述第二条,不是说了 dialog 一出现 onPause() 就会被调用么?看上去好像是前后矛盾,其实不然。

再来看,A dialog is always created and displayed as a part of an Activity 。也就是说 dialog 在 Android 系统中是不能单独存在的,只能作为 Activity 的一个组成部分。

就像我们设计的 Activity 中的其他元素,比如说按钮啊,各种 view 一样,只不过这个特殊的 view 在出现的时候其他元素将不能触碰到,而且颜色略暗而已。既然其只是 activity 中得一个元素而已,他的出现当然不会触发 Activity 的生命周期函数喽!

所以,我们可以认为:
文档里第二条所指的 Dialog 是系统级别的, 而我们平时所认为的 dialog 是 Activity 级别的,依附于 Activity。

------至所有正在努力奋斗的程序猿们!加油!!
有码走遍天下 无码寸步难行
1024 - 梦想,永不止步!
爱编程 不爱Bug
爱加班 不爱黑眼圈
固执 但不偏执
疯狂 但不疯癫
生活里的菜鸟
工作中的大神
身怀宝藏,一心憧憬星辰大海
追求极致,目标始于高山之巅
一群怀揣好奇,梦想改变世界的孩子
一群追日逐浪,正在改变世界的极客
你们用最美的语言,诠释着科技的力量
你们用极速的创新,引领着时代的变迁

——乐于分享,共同进步,欢迎补充
——Treat Warnings As Errors
——Any comments greatly appreciated
——Talking is cheap, show me the code
——诚心欢迎各位交流讨论!QQ:1138517609
——GitHub:https://github.com/selfconzrr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BugFree_张瑞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值