misaochan
misaochan

Reputation: 890

onRequestPermissionsResult() not called in fragment, tried all solutions I could find

onRequestPermissionsResult() is never called when I run my code. I'm have a method in my fragment that requests permissions:

void initiateGalleryUpload() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(getActivity(), READ_EXTERNAL_STORAGE) != PERMISSION_GRANTED) {
                if (shouldShowRequestPermissionRationale(READ_EXTERNAL_STORAGE)) {
                    new AlertDialog.Builder(getActivity())
                            .setMessage(getString(R.string.read_storage_permission_rationale))
                            .setPositiveButton("OK", (dialog, which) -> {
                                Timber.d("Requesting permissions for read external storage");
                                requestPermissions(new String[]{READ_EXTERNAL_STORAGE}, 1);
                                dialog.dismiss();
                            })
                            .setNegativeButton("Cancel", null)
                            .create()
                            .show();
                } else {
                    requestPermissions(new String[]{READ_EXTERNAL_STORAGE},
                            1);
                }
            } else {
                controller.startGalleryPick();
            }
        }
        else {
            controller.startGalleryPick();
        }

And another method in the same fragment that overrides onRequestPermissionsResult:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Timber.d("onRequestPermissionsResult: req code = " + " perm = " + permissions + " grant =" + grantResults);

    switch (requestCode) {
        // 1 = "Read external storage" allowed when gallery selected
        case 1: {
            if (grantResults.length > 0 && grantResults[0] == PERMISSION_GRANTED) {
                Timber.d("Call controller.startGalleryPick()");
                controller.startGalleryPick();
            }
        }
        break;

Common mistakes I have read up on but that don't apply to my case:

The permissions dialog appears as expected, and I can select "OK".

Would greatly appreciate some help with this as I have gone through a dozen StackOverflow posts to no avail. Complete code here if anyone needs it.

Upvotes: 1

Views: 212

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006594

In your activity-level onRequestPermissionsResult(), handle any permission requests issued by the activity.

If the requestCode does not match one requested by the activity (e.g., in a default branch for a switch), chain to the superclass (super.onRequestPermissionsResult(requestCode, permissions, grantResults);). If you are not doing this, that might prevent FragmentActivity from routing results to a fragment, for fragment-initiated requests.

Upvotes: 5

Related Questions