1

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?

yoonhok
  • 2,575
  • 2
  • 30
  • 58
  • Possible duplicte of https://stackoverflow.com/questions/31145255/android-scenario-where-onpause-is-called-but-not-onstop – Sonu Sanjeev Nov 18 '19 at 16:18

1 Answers1

3

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.

tynn
  • 38,113
  • 8
  • 108
  • 143
  • You mean, the official document is incorrect? "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." – yoonhok Nov 19 '19 at 00:18
  • Your example didn't open a **new** activity. A `Dialog` is not necessarily an activity. But an activity could act like a dialog. Basically I'm trying to shift your focus from the dialog to the activity only. Let the button start an activity which displays the dialog. – tynn Nov 19 '19 at 06:26
  • Yes, I agree. I only thought of "dialog" as AlertDialog, DialogFragment. – yoonhok Nov 19 '19 at 06:35