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

前端 未结 15 1273
余生分开走
余生分开走 2020-11-28 22:14

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 ite

相关标签:
15条回答
  • 2020-11-28 22:40

    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 .

    0 讨论(0)
  • 2020-11-28 22:40

    The method requestPermissions on Fragments requires API level 23 or higher.
    If your app targets a lower version you can use

    FragmentCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                ConstantVariables.READ_EXTERNAL_STORAGE);
    

    First you need to add the support-v13 dependency:

    implementation "com.android.support:support-v13:$supportLibVersion"
    
    0 讨论(0)
  • 2020-11-28 22:43

    Verify that both PERMISSION_REQUEST_CODE inonRequestPermissionsResult and inside yourFragment contains the same value.

    0 讨论(0)
  • 2020-11-28 22:44

    change this :

    ActivityCompat.requestPermissions(
        activity,
        arrayOf(Manifest.permission.READ_CONTACTS),
        PERMISSIONS_REQUEST_READ_CONTACTS
    )
    

    to this :

    requestPermissions(
         arrayOf(Manifest.permission.READ_CONTACTS),
         PERMISSIONS_REQUEST_READ_CONTACTS
    )
    
    0 讨论(0)
  • 2020-11-28 22:44

    This issue was actually being caused by NestedFragments. Basically most fragments we have extend a HostedFragment which in turn extends a CompatFragment. Having these nested fragments caused issues which eventually were solved by another developer on the project.

    He was doing some low level stuff like bit switching to get this working so I'm not too sure of the actual final solution

    0 讨论(0)
  • 2020-11-28 22:45

    In fragment you shouldn't use ActivityCompat for requestPermissions and you just use requestPermissions and pass two parameters in method.

    1. String for permission
    2. RequestCode.

    for exp :

     requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
    
    0 讨论(0)
提交回复
热议问题