Android: Under what circumstances would a Dialog appearing cause onPause() to be called?
Asked Answered
M

7

82

A snippet from the Android Activities document(scroll down to the "foreground lifetime" line) says :

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 don't quite understand this. Under what circumstances should this happen? Is onPause() called only if the context of the dialog in question is different from the activity on top of which the dialog is to be displayed?

EDIT: Adding code sample to illustrate my doubt in detail

Going by the above-mentioned quote from document, should my activity's onPause() method get called when the AlertDialog (or just the Dialog) in the following code gets displayed? Should I see the "onPause called" log entry when the dialog is displayed?

But I don't see that happen. And it shouldn't either, if I have understood the Android life cycle correctly! So, what's the document pointing at then?

public class LifeCycleTestActivity extends Activity {

    private static final String TAG = "LifeCycleTest";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick");

                AlertDialog dialog = new AlertDialog.Builder(LifeCycleTestActivity.this).create();
                 dialog.setMessage("You Clicked on the button");
                 dialog.setTitle("Dialog!");
                 dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                 dialog.setCancelable(true);
                 dialog.show();


                /*
                Dialog dialog = new Dialog(LifeCycleTestActivity.this);
                 dialog.setTitle("Dialog!");
                 dialog.setCancelable(true);
                 dialog.show();
                */
            }
        });        
    }

    @Override
    protected void onPause() {
        Log.d(TAG, "onPause() called");
        super.onPause();

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume() called");
    }
}
Madea answered 30/8, 2011 at 9:6 Comment(0)
E
201

onPause() is called when your activity is no longer at the top of the activity stack. A Dialog by itself is not an Activity, so will not replace the current Activity at the top of the stack, so will not cause anything to pause.

A dialog (lower-case) does not need to be implemented by a Dialog class, however. For example, it is not uncommon to implement one with an Activity whose theme is set to that of a dialog. In this case, displaying the dialog-as-an-Activity will cause the new Activity to be on the top of the stack, pausing what previously was there.

Enisle answered 12/9, 2011 at 7:52 Comment(5)
Great answer. The "dialog-as-an-activity" hadn't occurred to me. And the reasoning that the onPause() called only when the dialog is on top of the stack - that also explains why onPause() is not called even when the notification bar is pulled down to the bottom, completely obscuring the activity.Madea
@Madea There are so many subtleties here... Thanks to your question (and the answer of this amazing pro from the Android team) I have been able to understand quickly why I am observing in my application exactly what you observed. +1.Wildon
@Enisle : So will a system dialog cause onPause(). I'm seeing this while looking at marshmallow permission related system dialogs!Goldman
@Goldman That is probably because the Permission dialog is implemented as an Activity with the theme set to that of a DialogMadea
Hi All, I have one question for me Dialog not showing when the current activity is onStop() state in AOSP 10. But same state was in Android 6 working fine. Please let me know why not showing in Android 10.Compendious
N
13

I've been doing quite a lot of code with dialogs, including the AlertDialog that you mention, and I've also tried to check if onPause() is being called on the activity when the dialog pops up, but thus far my conclusion is that the activity simply keeps running and that onPause() is not called.

Not sure if it helps, but at least you now know that there are others who experience what you're experiencing :-)

Namecalling answered 8/9, 2011 at 9:43 Comment(1)
Well, actually, if I understand the life cycle correctly, I believe a dialog MUST NOT trigger the onPause(). So the observed behavior is consistent with what is expected. It is just that I stumbled upon quite by chance, this piece of information that I've quoted and linked to. And that led me down the path of confusion!Madea
U
2

Its wrong that activity remains no longer at top of activity stack in onPause phase.

Condition an activity to be onPause state -

  • Activity partially visible e.g. dialog on activity.

  • The Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager.

    e.g Home button pressed causes activity to go in onPause(). Still at top of stack.

In fig 1. Activity3 will be destroyed and removed from top stack

In fig 2. Now Task A goes to background but Activty X still on top of stack . If you override onPause() method int this state

enter image description here

Figure 1. A representation of how each new activity in a task adds an item to the back stack. When the user presses the Back button, the current activity is destroyed and the previous activity resumes.

enter image description here

Figure 2. Two tasks: Task B receives user interaction in the foreground, while Task A is in the background, waiting to be resumed.

Ungula answered 7/6, 2014 at 2:34 Comment(1)
In the scenario you explained, the Activity is still at top of the stack, but the task itself has gone to the background. Basically the user is no longer interacting with your Activity - some other Activity (either from your own task or from some other) has come to the front. That is what the accepted answer is trying to explain.Madea
C
0

I think I remember reading in an earlier version of the Android Lifecycle that onPause was called when none of the activity is on display. i.e. if a bit of your activity is still visible under a popup, onPause will not be called.

Maybe some other experts can vouch for this behavior?

Carltoncarly answered 6/9, 2011 at 22:10 Comment(1)
The bit about onPause() being called if none of the Activity is on display - that's generally true, but not strictly so. For instance, when you pull down the notification bar such that it completely obscures your Activity, onPause() is still not called.Madea
K
0

In my slightly weird experience onResume gets called with dialog.setCanceledOnTouchOutside(true); but onPause never gets called.

That being said, I think the documentation might focus on system dialogs (e.g. low on battery).

Kailyard answered 28/2, 2012 at 9:54 Comment(0)
S
0

@hackbot

onPause() is called when your activity is no longer at the top of the activity >stack. A Dialog by itself is not an Activity, so will not replace the current >Activity at the top of the stack, so will not cause anything to pause.

everything depends on implementation...

what is a Dialog ? is a Window added to Display by WindowManager/// so the window when it shows is on top of everything .... (Z order)

what is activity... is "thing" that also creates its window....

when a dialog is shown or it's window comes visible on top of an existing activity, then it overrides partial the activity window so existing activity will move to partially invisible state and you will get call to onPause() from ActivityThread.

but to be sure we also need to consider here a one think...

the state of window if is a standalone window shown on top or it is a child window and a parent of it is a activity window....

so when we know

  • the Window.LayoutParams (FLAGS) we use to add
  • and what IBinder is used for the Window to show

we will khow how the activity will behave when windows are shown each over other .. as each winndow has a callbacks they are used by activity or dialog to manage their states...

involved components:

  • android.os.IBinder

  • android.view.Window

  • android.view.Window.Callback

  • android.view.WindowManager

  • android.view.WindowManager.LayoutParams

  • android.view.Display

btw:

if you want to know the windows on screen [ applicable only for the process you own - as window belongs to process and those are Sandboxed - each processs is a separate JVM strictly saying "ART" ] you can use a replection see :

  • android.view.WindowManagerImpl
  • android.view.WindowManagerGlobal
Stadia answered 5/7, 2018 at 20:29 Comment(0)
S
-4

onPause() is called every Time when an Activity goes background and Dialog or other Activity comes foreGround. This is done to give first priority to something with which the user is interacting. e.g: assume you are in homescreen (which in turn is an activity) of an application, the homescreen is said to be in foreground. and when you go to next screen by pressing some button or a dialog appears the next screen/Activity/Dialog comes to foreGround and homecreen goes to backGround, which just means homeScreen's onPause() method got called.

Shool answered 6/9, 2011 at 10:45 Comment(2)
Well the case where another Activity comes to the foreground is obvious. I am confused by the document's claim that onPause() is called when a dialog appears. I have edited my question with code explaining what I think the document claims, but I don't see happening.Madea
@techie.curious..yeah may be you are right. it happens only with Activity transition and not when Dialog gets created.. i checked it now.Shool

© 2022 - 2024 — McMap. All rights reserved.