Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onRequestPermissionsResult not being called in fragment if defined in both fragment and activity

I have a fragment in which I have recyclerview and setting data in this recyclerview using recyclerview adapter.

Now, I am having a button in the adapter's list item clicking on which I need to check the READ_EXTERNAL_STORAGE permission in android for new permission model in android.

I have created a new function in this adapter's fragment to check if permission is granted or not and request for permission if not granted already.

I have passed MyFragment.this as a parameter in the adapter and calling the fragment's method on the button click in the adapter.

I have used the below code to call requestPermission in fragment.

if(ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED){
       requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                ConstantVariables.READ_EXTERNAL_STORAGE);
    }

I have overridden the onRequestPermissionsResult method in fragment by using the below code:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case ConstantVariables.READ_EXTERNAL_STORAGE:
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, proceed to the normal flow.
                startImageUploading();
            } else {}

But it is not getting called, instead of this Activity's onRequestPermissionsResult method is getting called.

I have defined the same onRequestPermissionsResult method in fragment's parent activity also and it is getting called.

I can not remove the activity's onRequestPermissionsResult method but want to call fragment's onRequestPermissionsResult method when I request permission from fragment. How can I do this? Am I doing something wrong here, please help me if anyone have idea here.

like image 530
Prithniraj Nicyone Avatar asked Mar 14 '16 13:03

Prithniraj Nicyone


People also ask

How do I use ActivityCompat?

Sets the permission delegate for ActivityCompat . Gets whether you should show UI with rationale before requesting a permission. Start new activity with options, if able, for which you would like a result when it finished. Start new IntentSender with options, if able, for which you would like a result when it finished.

What is fragmentactivity onrequestpermissionsresult?

FragmentActivity.onRequestPermissionsResult (..) is where the call to fragment's onRequestPermissionsResult (..) is forwarded to. @Bonatti So if i try to request a permission in android 4.4.2 i won't get a result? Thus I cannot change the permissions , only see them? @AlexanderFragotsis You will get a result.

How do I get permission for an Android fragment?

When in android.support.v4.app.Fragment, you should use simply requestPermissions (this is an instance method of android.support.v4.app.Fragment) If you call ActivityCompat.requestPermissions in a fragment, the onRequestPermissionsResult callback is called on the activity and not the fragment. Hope this helps!

How to stop onrequestpermissionsresult() from being called?

in your AndroidManifest.xml then onRequestPermissionsResult () will not be called. This is true if your Activity is derived from Activity or AppCompatActivity. This can be fixed by removing both flags from 'AndroidManifest.xml' and finishing your activity with finishAndRemoveTask () instead.

When startactivityforresult() is called from an inner fragment?

Sometimes it may happen that according to the need of Android application development, we have to implement multiple or nested fragments in our application and in those cases, when startActivityForResult () is called from an inner fragment, we do not receive the activity result.


Video Answer


3 Answers

Edited answer to cover broader issues

I think you are confusing the method for fragment and activity. Had a similar issue my project last month. Please check if you have finally the following:

  1. In AppCompatActivity use the method ActivityCompat.requestpermissions
  2. In v4 support fragment you should use requestpermissions
  3. Catch is if you call AppcompatActivity.requestpermissions in your fragment then callback will come to activity and not fragment
  4. Make sure to call super.onRequestPermissionsResult from the activity's onRequestPermissionsResult.

See if it helps .

like image 85
VarunJoshi129 Avatar answered Oct 08 '22 05:10

VarunJoshi129


I requested location permission from a fragment and in the fragment, I needed to change this:

            ActivityCompat.requestPermissions(getActivity(), new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_REQ_CODE);

to this:

            requestPermissions(new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_REQ_CODE);

then the onRequestPermissionsResult was called in the fragment.

like image 164
fullmoon Avatar answered Oct 08 '22 05:10

fullmoon


This is a common mistake that people make when coding for marshmallow.

When in AppCompatActivity, you should use ActivityCompat.requestPermissions; When in android.support.v4.app.Fragment, you should use simply requestPermissions (this is an instance method of android.support.v4.app.Fragment) If you call ActivityCompat.requestPermissions in a fragment, the onRequestPermissionsResult callback is called on the activity and not the fragment.

requestPermissions(permissions, PERMISSIONS_CODE);

If you are calling this code from a fragment it has it’s own requestPermissions method.

So basic concept is, If you are in an Activity, then call

ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.CAMERA},
                            MY_PERMISSIONS_REQUEST_CAMERA);

and if in a Fragment, just call

requestPermissions(new String[]{Manifest.permission.CAMERA},
                            MY_PERMISSIONS_REQUEST_CAMERA);

For the reference, I have obtained the answer from this link https://www.coderzheaven.com/2016/10/12/onrequestpermissionsresult-not-called-on-fragments/

like image 39
Farruh Habibullaev Avatar answered Oct 08 '22 04:10

Farruh Habibullaev