简体   繁体   English

Android 活动生命周期 - 何时调用“onPause”?

[英]Android Activity LifeCycle - when 'onPause' is called?

I am an Android app developer.我是 Android 应用程序开发人员。

I thought I know the life cycle of the activity.我以为我知道活动的生命周期。

But... I am confused now.但是……我现在很困惑。

As per the official docs: https://developer.android.com/guide/components/activities/activity-lifecycle.html#onpause根据官方文档: https://developer.android.com/guide/components/activities/activity-lifecycle.html#onpause

'onPause' is called when partially invisible.部分不可见时调用“onPause”。

A new, semi-transparent activity (such as a dialog) opens.一个新的、半透明的活动(例如对话框)打开。 As long as the activity is still partially visible but not in focus, it remains paused.只要活动仍然部分可见但不在焦点上,它就会保持暂停状态。

So I have thought that if a dialog is opened, then the activity is paused.所以我认为如果打开一个对话框,那么活动就会暂停。

I made some sample code to prove this.我做了一些示例代码来证明这一点。

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn.setOnClickListener {
//            1. AlertDialog
//            AlertDialog.Builder(this)
//                .setTitle("TEST")
//                .show()

//            2. DialogFragment
//            val dialog = TestFragment()
//            supportFragmentManager.beginTransaction().add(dialog, "").commit()
        }
    }

    override fun onPause() {
        super.onPause()
        Log.d("TEST", "[LifeCycle] onPause")
    }

When I clicked the "btn", Dialog/DialogFragment is opened.当我单击“btn”时,会打开 Dialog/DialogFragment。 But 'onPause' log was not printed out.但是“onPause”日志没有打印出来。

I am confused...我很困惑...

Does the official document incorrect?官方文档有误吗?

It also states that它还指出

The system calls this method as the first indication that the user is leaving your activity [...] it indicates that the activity is no longer in the foreground系统调用此方法作为用户离开您的活动的第一个指示 [...] 它表明活动不再在前台

A dialog within your own activity will not pause it.您自己的活动中的对话框不会暂停它。 Only a new activity containing the dialog would pause the first one.只有包含对话框的新活动才会暂停第一个活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未调用Activity生命周期方法的onPause()和onStop() - onPause() and onStop() of Activity lifecycle methods not being called 当活动处于 onPause 生命周期时尝试运行 aynktask - try to run asynktask when activity is in onPause lifecycle Android:在Activity生命周期中何时调用onCreateOptionsMenu? - Android: When is onCreateOptionsMenu called during Activity lifecycle? 活动生命周期的奇怪行为 - 在onResume()之后也onPause()调用...为什么? - Weird behaviour of activity lifecycle - after onResume() also onPause() called …why? Android Activity Lifecycle在启动新Activity时调用两次 - Android Activity Lifecycle called twice when starting new Activity Android Java:称为AFTER onPause()的活动 - Android Java: Activity called AFTER onPause( ) Android活动生命周期:onResume(),onStart(),onPause()和onStop()上的工作负载 - Android Activity LifeCycle: Workload on onResume(), onStart(), onPause() and onStop() 在片段之间切换时会调用Activity onpause吗 - Will Activity onpause be called when switching between fragments onPause 是否保证在 Activity 不再运行时调用? - Is onPause guaranteed to be called when an Activity is no longer running? 在Activity上调用onPause时,数据会发生什么 - What happens to data when onPause is called on Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM