两种方案:
- 宿主Activity中
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// 获取到Activity下的Fragment
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments == null)
{
return;
}
// 查找在Fragment中onRequestPermissionsResult方法并调用
for (Fragment fragment : fragments)
{
if (fragment != null)
{
// 这里就会调用我们Fragment中的onRequestPermissionsResult方法
fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
2.Fragment 中 只用如下方式请求权限
fragment.requestPermissions(mPermissionList,REQUEST_CODE_CAMERA)
而不是 ActivityCompat/ContextCompat
ActivityCompat.requestPermissions((AppCompatActivity)context, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE)