I've following MainActivity which seeks permission for 6.0 Phone & Storage tasks. It prompts for permission 2x times and then crashes. Do I need to re-start the MainActivity after I acquired_permission? thanks for any help or pointers.
MainActivity.java
onCreate(){
seek_permissions()
//load db data and continue
//with the app tasks
}
public void seek_permissions(){
boolean hasWritePermission = (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasWritePermission) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.MOUNT_FORMAT_FILESYSTEMS,Manifest.permission.RECEIVE_BOOT_COMPLETED,Manifest.permission.MODIFY_PHONE_STATE}, REQUEST_PERM);
}
}
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERM:
{
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
acquired_perm = 1;
}
else {Toast.makeText(MainActivity.this, "Please consider granting these permissions", Toast.LENGTH_LONG).show(); }
}
return;
}
}
To check if the user has already granted your app a particular permission, pass that permission into the ContextCompat. checkSelfPermission() method. This method returns either PERMISSION_GRANTED or PERMISSION_DENIED , depending on whether your app has the permission.
For me this was caused by using android:noHistory="true"
in my manifest for this activity?
I believe this may be a bug with Android 6.0 as later versions work fine it seems.
If noHistory
is set the ActivityCompat.requestPermissions
call causes the onDestroy
of the calling Activity to be executed thus destroying. As it isn't saved in the history due to the noHistory
it no longer exists and the app is essentially closed when permission dialogue is dismissed.
As a workaround instead of "noHistory"
in the manifest you can call finish()
in onStop()
or another appropriate location when navigating away from the Activity and remove the tag from your manifest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With