Duplicate permission request after orientation change

和自甴很熟 提交于 2020-01-01 02:33:05

问题


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?


回答1:


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;
    }
}



回答2:


As @user1991776 mentioned there is actually an undocumented extra that contains whether or not there is a permission dialog open at the moment, in Activity:

private static final String HAS_CURENT_PERMISSIONS_REQUEST_KEY =
        "android:hasCurrentPermissionsRequest";

However there is a better way. When you request a permission dialog the second time (due to a rotation), Activity automatically cancels the old dialog by calling your onRequestPermissionResult() with empty arrays:

public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
    if (mHasCurrentPermissionsRequest) {
        Log.w(TAG, "Can reqeust only one set of permissions at a time");
        // Dispatch the callback with empty arrays which means a cancellation.
        onRequestPermissionsResult(requestCode, new String[0], new int[0]);
        return;
    }
    Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
    startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
    mHasCurrentPermissionsRequest = true;
}

Or course this behaviour isn't documented because this is Android, and who wants to document complex behaviour?

Anyway you can just always request permissions in onCreate() and then ignore calls to onRequestPermissionsResult() with zero-length permissions arrays.




回答3:


I guess as this is a system dialog you cannot control it. You could instead prevent that your activity gets reloaded if you turn your device.



来源:https://stackoverflow.com/questions/32227707/duplicate-permission-request-after-orientation-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!