I'm upgrading the app to target android 12, before its targeting android 10. So, now I'm using MANAGE_EXTERNAL_STORAGE with registerForActivityResult but when user allow access to manage files and press back button the registerForActivityResult do not get called. Here is my code.
In onCreate method
registerLauncher();
if (checkPermissionsGranted()) {
initMainActivity();
} else {
requestPermission();
}
The above methods in sequence.
private void registerLauncher() {
permissionLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Log.e(TAG, "inside activity result");
initMainActivity();
}
}
});
}
private boolean checkPermissionsGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
int readPermission = ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE);
int writePermission = ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE);
// int storagePermission = ContextCompat.checkSelfPermission(mContext, Manifest.permission_group.STORAGE);
return readPermission == PackageManager.PERMISSION_GRANTED && writePermission == PackageManager.PERMISSION_GRANTED;
}
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
try {
// Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
// Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
// startActivity(intent);
// permissionLauncher.launch(intent);
// startActivityForResult(intent, AppConstants.STORAGE_PERMISSION_CODE);
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
// startActivityForResult(intent, AppConstants.STORAGE_PERMISSION_CODE);
permissionLauncher.launch(intent);
} catch (Exception e) {
Log.e(TAG, "Exp... " + e.toString());
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
permissionLauncher.launch(intent);
}
} else {
ActivityCompat.requestPermissions(Splashscreen.this, new String[]{Manifest.permission_group.STORAGE}, AppConstants.STORAGE_PERMISSION_CODE);
}
}
I also tried with startActivityForResult(intent, AppConstants.STORAGE_PERMISSION_CODE); (I know startActivityForResult is deprecated) with overriding onRequestPermissionsResult but none of these two get called
1 - registerForActivityResult
2 - onRequestPermissionsResult
Please let me know what I'm doing wrong here? As I'm check the permission on splash activity, the app get stuck on the splash as onResult is not being called.
Thanks & Regards