Cancel AlertDialog in onPause method of Activity in Android

3k views Asked by At

following is my code for AlertDialog,

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,int which) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        mContext.startActivity(intent);
       }
 });

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
       dialog.cancel();
       }
   });
alertDialog.show();

@Override
protected void onPause() {
    super.onPause();
 // I want dialog to be dismissed here

}

Its working fine but what i want is to dismiss the dialog in onPause method of Activity,
how can i achieve it ?

3

There are 3 answers

6
MysticMagicϡ On BEST ANSWER

Use this in onPause

alertDialogforGPS.dismiss();

EDIT:

AlertDialog.Builder builder;

builder = new AlertDialog.Builder(WelcomeActivity.this).setTitle("Title")
.setMessage("Message")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int which) {
        //your code
    }
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});
AlertDialog dialog = builder.create();
dialog.show();

And in onPause

dialog.dismiss();
0
Terry On

First, you have to make the AlertDialog.Builder alertDialog a member of your class. Like:

private AlertDialog.Builder mAlertDialog;

Then, at the point where you define your alert dialog, you use this member:

mAlertDialog = new AlertDialog.Builder(mContext);
mAlertDialog.setTitle("GPS settings");
mAlertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
mAlertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int which) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        mContext.startActivity(intent);
    }
});

mAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});
mAlertDialog.show();

And then in the onPause() method, you can dismiss the dialog.

@Override
protected void onPause() {
    if (mAlertDialog != null) {
        mAlertDialog.dismiss();
    }
    super.onPause();
}
3
Pragnesh Ghoda  シ On

Try This..

Declare global variable..

AlertDialog.Builder alertDialog;

Define and Access it when you need it.

alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,int which) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
       dialog.dismiss(); // NOTE : dismiss dialog before moving to other activity.. it will prevent causing exception... 
        mContext.startActivity(intent);
       }
 });

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
       dialog.dismiss();
       }
   });

accessing variable in other methods..

@Override
protected void onPause(){
    //checking for null variable
    if(alertDialog != null)
        alertDialog.dismiss();
    }
}