B
B
B0nG2019-05-22 21:46:16
Java
B0nG, 2019-05-22 21:46:16

How to close the window?

Hello! I am writing an application, I have reached the stage of checking whether GPS is enabled or not, if it is enabled, I go further according to the logic of the program, if not, I display a dialog and transfer the user to the location settings.
Dialog code:

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("GPS выключен, включить?")
                .setCancelable(false)
                .setPositiveButton("Да", new DialogInterface.OnClickListener() {
                    public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        dialog.cancel();
                        startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                        checkGpsStatus();
                    }
                })
                .setNegativeButton("Нет", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                        checkGpsStatus();
                    }
                });
        final AlertDialog alert = builder.create();
        alert.show();

The problem is that the dialog appears when I click on "YES" - it takes me to the location settings, there I turn on gps, then I press the "back" button and it throws me back to my program, but the dialog is not closed but open , when you click on "no" - the program crashes, when you click on "yes" the settings open again, when you click "back" the program crashes. What's wrong? How to win it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question