Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate permission request after orientation change

Because the Android SDK 23 gives users the possibility to deny apps access to certain functionalities I wanted to update one of my apps to request permissions as it is described in here: https://developer.android.com/preview/features/runtime-permissions.html.

In one of the activities I embed a SupportMapFragment. To make it work you need to have the WRITE_EXTERNAL_STORAGE permission, so I request it when I start the activity which results in a creation of a permission request dialog.

Now the problem is that when the dialog is still open and I rotate the device the activity will be restarted and open a new permission request dialog while the old one is still there. The result is two of those dialogs on top of each other and only one of it being useful.

Is there a way to get rid of the dialog that was started first?

like image 754
Thorben Avatar asked Aug 26 '15 13:08

Thorben


People also ask

What happens when screen orientation changes in Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

How do you handle a dysfunction in screen reorientation?

Prevent Activity to recreated Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

How to check if a permission is granted Android?

To check if the user has already granted your app a particular permission, pass that permission into the ContextCompat. checkSelfPermission() method. This method returns either PERMISSION_GRANTED or PERMISSION_DENIED , depending on whether your app has the permission.


Video Answer


1 Answers

As CommonsWare said in his comment the best solution is to put a boolean into the savedInstanceState-Bundle to know if the dialog is still open.

Example:

// true if dialog already open
private boolean alreadyAskedForStoragePermission = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState != null) {
        alreadyAskedForStoragePermission = savedInstanceState.getBoolean(STORAGE_PERMISSION_DIALOG_OPEN_KEY, false);
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putBoolean(KEY, alreadyAskedForStoragePermission);
}

private void checkStoragePermission(){
    if(alreadyAskedForStoragePermission){
        // don't check again because the dialog is still open
        return;
    }

    if(ActivityCompat.checkSelfPermission(this, STORAGE_PERMISSIONS[0]) != PackageManager.PERMISSION_GRANTED){
        // the dialog will be opened so we have to keep that in memory
        alreadyAskedForStoragePermission = true;
        ActivityCompat.requestPermissions(this, STORAGE_PERMISSIONS, STORAGE_PERMISSION_REQUEST_CODE);
    } else {
        onStoragePermissionGranted();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case STORAGE_PERMISSION_REQUEST_CODE:
            // the request returned a result so the dialog is closed
            alreadyAskedForStoragePermission = false;

            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                onStoragePermissionGranted();
            }

            break;
    }
}
like image 54
Thorben Avatar answered Oct 02 '22 12:10

Thorben