简体   繁体   中英

Android Activity LifeCycle - when 'onPause' is called?

I am an Android app developer.

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

'onPause' is called when partially invisible.

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. But 'onPause' log was not printed out.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM