Request permission dialog pauses my activity

后端 未结 3 1008
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 15:47

I am asking for permission inside onActivityResult of my activity and what is happening is that my activity is being paused while request permission dialog is disp

3条回答
  •  -上瘾入骨i
    2021-01-22 16:26

    Source code of method Activity#requestPermissions:

    public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
        if (requestCode < 0) {
            throw new IllegalArgumentException("requestCode should be >= 0");
        }
        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;
    }
    

    We can clearly see that a new activity is opened. Hence onPause in the calling activity will be surely called. So this is the expected behavior.

    If you want to prevent pausing your activity, make sure you already have the required permissions before opening that activity.

    If your activity is main activity, add an splash activity. Otherwise check for permissions before opening your activity.

提交回复
热议问题