onRequestPermissionsResult called in HomeActivity but not in Fragment

不羁的心 提交于 2020-01-05 05:25:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!