curioustechizen curioustechizen - 2 years ago 112
Android Question

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

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
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
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
method get called when the
(or just the
) 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. */
public void onCreate(Bundle savedInstanceState) {

Button btn = (Button) findViewById(;

btn.setOnClickListener(new OnClickListener() {

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.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

Dialog dialog = new Dialog(LifeCycleTestActivity.this);

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


protected void onResume() {
Log.d(TAG, "onResume() called");

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download