问题
In a fragment I call requestPermissions
public class FgDialogFragment extends DialogFragment {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},22);
In the HomeActivity. java I wrote this code which is working fine
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
Log.i("info", "Result recieved");
}
but I want to get onRequestPermissionsResult on the FgDialogFragment class. I tried this but it's not working. There is a lot of similar topics but none of them helped me to solve my problem.
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
Log.i("info", "Result recieved in fragment");
}
回答1:
The requestPermissions
call is only available within an Activity. There is also an ActivityCompat.requestPermissions
, but that also takes an Activity
context for the callback.
The callback will only work within an activity. I suggest either you create a listener you can call into your fragment or use an event bus.
来源:https://stackoverflow.com/questions/39729377/onrequestpermissionsresult-called-in-homeactivity-but-not-in-fragment