4

According to the documentation:

An activity can frequently transition in and out of the foreground—for example, onPause() is called when the device goes to sleep or when a dialog appears.

I have an activity with a button. When I tap on the button a dialog appears. I expected onPause method was called when the dialog comes up and then onResume method called when the dialog is dismissed. But none of them are called.

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            MyDialog myDialog = new MyDialog();
            myDialog.show(getFragmentManager(),myDialog.TAG);


        }
    });

@Override
protected void onResume() {
    super.onResume();

    Toast.makeText(this,"ON RESUME ACTIVITY",Toast.LENGTH_SHORT).show();

}

@Override
protected void onPause() {
    super.onPause();

    Toast.makeText(this,"ON PAUSE ACTIVITY",Toast.LENGTH_SHORT).show();

}

Does anyone know why are these methods not being called? Thanks

theWizard
  • 41
  • 1
  • 2
  • 3
    it's called when the activity is onResumed not in dialog – Zied R. Jul 24 '15 at 09:58
  • 1
    Look at [this](http://stackoverflow.com/questions/7240916/android-under-what-circumstances-would-a-dialog-appearing-cause-onpause-to-be#7384782). It should answer your question. – Exception Jul 24 '15 at 10:01

1 Answers1

6

Dialog is also a part of your activity UI window. So onPause() & onResume() will not get called when you display or hide a dialog.

If you want onPause() & onResume() to get called when you show/hide a dialog, then display that dialog UI in a separate activity, and start that activity as dialog.

ghchoi
  • 4,812
  • 4
  • 30
  • 53
Audumbar
  • 404
  • 5
  • 16