I'm a beginner in android, and would appreciate any help.
Basically in androidx.fragment.app.Fragment
after calling requestPermissions()
, onRequestPermissionsResult()
is never called.
I read on another question (onRequestPermissionsResult not being called in fragment if defined in both fragment and activity) that ActivityCompat.requestPermissions
would result in activity's onRequestPermissionsResult
method being called. and simple requestPermissions
would call onRequestPermissionsResult
in same fragment. I've tried both ways but every time activity's method is being called i think maybe because this is not a v4 support fragment but a androidx.fragment.app.Fragment
.
I tried overriding onRequestPermissionsResult
in activity and called super's method but still fragment's method is not being called.
private final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"};
private int REQUEST_CODE_PERMISSIONS = 10;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setupTimeOut();
if (allPermissionsGranted()) {
// Do stuff that requires permission
} else {
requestPermissions(REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (allPermissionsGranted()) {
// Do stuff that requires permission
} else {
Toast.makeText(getViewContext(), "Permissions not granted", Toast.LENGTH_SHORT).show();
}
}
}
private boolean allPermissionsGranted() {
//check if req permissions have been granted
for (String permission : REQUIRED_PERMISSIONS) {
if (ContextCompat.checkSelfPermission(getViewContext(), permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
what i want is to get the permissions result callback in same fragment.
In case this is relevant, permissions are for camera and write external storage and fragment is a simple photo capture fragment.