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

AndroidDialogLifecycleAndroid LifecycleOnpause

Android Problem Overview


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");
    }
}

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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 :-)

Solution 3 - Android

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.

Solution 4 - Android

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?

Solution 5 - Android

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).

Solution 6 - Android

@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

Solution 7 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncurioustechizenView Question on Stackoverflow
Solution 1 - AndroidhackbodView Answer on Stackoverflow
Solution 2 - AndroidMichell BakView Answer on Stackoverflow
Solution 3 - AndroidYuvraj KakkarView Answer on Stackoverflow
Solution 4 - AndroidFrinkTheBraveView Answer on Stackoverflow
Solution 5 - AndroidKarlView Answer on Stackoverflow
Solution 6 - Androidceph3usView Answer on Stackoverflow
Solution 7 - AndroidngeshView Answer on Stackoverflow