How to introduce in this AlertDialog a onBackButtonPressed method?

82 views Asked by At

I have this alertDialog, but when I click on the back button nothing happens:

public class LocationQueries {
    Activity context;

    public LocationQueries(Activity context){
        this.context = context;
    }
    protected void promptUserToActivateGps() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder
                .setMessage(
                        "GPS is disabled in your device. Would you like to enable it?")
                .setCancelable(false)
                .setPositiveButton("Enable GPS",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent callGPSSettingIntent = new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                context.startActivity(callGPSSettingIntent);
                            }
                        });

        alertDialogBuilder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                            promptUserToActivateGps();
                    }
                });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }
}

How can I introduce a onBackButtonPressed method ?

Update: I updated the code. When I use it in an activity I do this :

   private LocationQueries locationQueries = new LocationQueries(this); 

locationQueries.promptUserToActivateGps(); 
1

There are 1 answers

6
Plasma On BEST ANSWER

Update: This is one way I can think of to achieve what you're trying to do. Basically, pass the AlertDialog back to the Activity in the Activity's onBackPressed-method and do the same thing as my previous answer. Here's the code:

public class LocationQueries {
    Activity context;
    AlertDialog alert; // Declare the dialog

    public LocationQueries(Activity context){
        this.context = context;
    }
    protected void promptUserToActivateGps() {
        /* Your method, except change this: */
        alert = alertDialogBuilder.create();
        alert.show();
    }

    // Add a getter for your dialog
    public AlertDialog getAlertDialog() {
        return alert;
    }
}

public class YourActivity extends Activity {
    /* Your other code */
    private LocationQueries locationQueries = new LocationQueries(this);

    @Override
    public void onBackPressed() {
        AlertDialog alert = locationQueries.getAlertDialog();
        if(alert != null && alert.isShowing()) {
            alert.getButton(DialogInterface.BUTTON_NEGATIVE).performClick();
        }
    }
}

Final edit: Turns out there's an easier way to do this, as described in alexc's answer here, by using the AlertDialog's setOnCancelListener, which is invoked when pressing the back button while the dialog is open, as long as the AlertDialog.Builder.setCancelable() is set to true.