Search code examples
androidandroid-permissionsandroid-maps

How can do when the user reject permissions in the app?


I am developing an app which the main function is a see data on a map.

For this, I need to get the current position, and for this, I request to user.

But if the user rejects the permissions, what is the best way to ask again for it?

I try asking again in onRequestPermissionsResult but the app crash.

Any idea?

Code:

private void enableMyLocationIfPermitted() {    
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
        if (mapViewModel.getGoogleMap() != null) {
            mapViewModel.getGoogleMap().setMyLocationEnabled(true);
        } else {
            Log.e(TAG, "No Google Map available in view model");
        }
     } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
     }

}

@Override
public void onMapReady(GoogleMap googleMap) {
    Log.d(TAG, "Map is ready");
    googleMap.setOnMarkerClickListener(this);
    googleMap.getUiSettings().setRotateGesturesEnabled(false);
    googleMap.getUiSettings().setMyLocationButtonEnabled(false);
    mapViewModel.setGoogleMap(googleMap);
    enableMyLocationIfPermitted();
    getLastLocationIfPermitted();
    mapViewModel.loadData();
}


@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case LOCATION_PERMISSION_REQUEST_CODE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    enableMyLocationIfPermitted();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
                Toast.makeText(this, getResources().getText(R.string.you_need_permissions), Toast.LENGTH_LONG).show();
            }
            return;
         }

     }
}

Error:

java.lang.StackOverflowError: stack size 8MB

Thanks


Solution

  • If the user denied permission with "Never ask again" option, your onRequestPermissionsResult method would call itself forever until it throws StackOverFlowError.

    The best thing to do here is to not ask permission again in the onRequestPermissionsResult method once user denies it. Rather you can disable the feature or you can open the permission settings of your app and have them manually change the permission settings using below code.

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case LOCATION_PERMISSION_REQUEST_CODE: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    enableMyLocationIfPermitted();
                } else {
                    Toast.makeText(this, getResources().getText(R.string.you_need_permissions), Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                }
                return;
            }
    
        }
    }
    

    If you want to read more on the permissions https://developer.android.com/training/permissions/requesting this is a good guide.