Karumi / Dexter

Android library that simplifies the process of requesting permissions at runtime.
http://karumi.com
Apache License 2.0
5.23k stars 671 forks source link

Permission dialog isn't shown after turning off the premission manually #223

Closed burekas7 closed 4 years ago

burekas7 commented 5 years ago

Background:

  1. The Dexter is a fragment inside ViewPager inside the main activity.
  2. Requesting for "Locations" permissions.

Scenarios:

  1. [OK] On the first time the app is running, Location is disabled, the dialog is shown up (onPermissionsChecked is activate and confirm the dialog does the work)

  2. [OK] In the second time opening the app, Location is already enabled, dialog isn't shown up as it should be, and everything is ok.

  3. [The problem] While the app is still running and I turn off the Location permission, when checking the permissions the "onPermissionRationaleShouldBeShown" is activate. a. Why the dialog isn't shown up again? b. Why it goes to "onPermissionRationaleShouldBeShown" callback?

  4. [Still relates to the problem] In this callback I'm opening the permissions screen of my app, there I'm turn on the Location permission, and then press 'Back' until I return to the app, when checking the permissions again, non of the callbacks is called, neither "onPermissionsChecked" nor "onPermissionRationaleShouldBeShown". Why?

  5. [OK] Only when I clear the data of the app or reinstall it again, the dialog is shown as in (1).

Version of the library

5.0.0

Code

    private void registerLocationUpdates() {
        Dexter.withActivity(getActivity())
                .withPermissions(Manifest.permission.ACCESS_COARSE_LOCATION,
                                 Manifest.permission.ACCESS_FINE_LOCATION)
                .withListener(new MultiplePermissionsListener() {
                    @Override public void onPermissionsChecked(MultiplePermissionsReport report) {
                        // check if all permissions are granted
                        if (report.areAllPermissionsGranted()) {
                            getMyLocation();
                        }
                        // check for permanent denial of any permission
                        if (report.isAnyPermissionPermanentlyDenied()) {
                            // permission is denied permenantly, navigate user to app settings
                        }
                    }
                    @Override public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
                        showSettingsDialog();
                    }
                }).check();
    }

    /**
     * Showing Alert Dialog with Settings option
     * Navigates user to app settings
     * NOTE: Keep proper title and message depending on your app
     */
    private void showSettingsDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Need Permissions");
        builder.setMessage("This app needs permission to use this feature. You can grant them in app settings.");
        builder.setPositiveButton("GOTO SETTINGS", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                openSettings();
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();

    }

    // navigating user to app settings
    private void openSettings() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", MyApp.getContext().getPackageName(), null);
        intent.setData(uri);
        startActivityForResult(intent, 101);
    }
ghost commented 5 years ago

I solved the problem with adding bellow code "token.continuePermissionRequest();"

` @Override public void onPermissionRationaleShouldBeShown(List permissions, PermissionToken token) {

                    token.continuePermissionRequest();
                    showSettingsDialog();

                }

`

Serchinastico commented 4 years ago

This behavior is described in our README and the Android documentation for runtime permissions and we are just bypassing it so that users can decide what to do.