I am asking for permission inside onActivityResult
of my activity and what is happening is that my activity is being paused while request permission dialog is displayed. Why is that and can I somehow prevent it?
All I doing is asking for permission in a normal way:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_LOCATION_ACCESS_CODE);
requestPermissions docs says
Although an another solution is move your code in
onStop
if possible and suitableSource code of method Activity#requestPermissions:
We can clearly see that a new activity is opened. Hence
onPause
in the calling activity will be surely called. So this is the expected behavior.If you want to prevent pausing your activity, make sure you already have the required permissions before opening that activity.
If your activity is main activity, add an splash activity. Otherwise check for permissions before opening your activity.
There is also possibility that, your Activity (say, MainActivity) that invoking the
might have been declared flag
in the Manifest.
Therefore the MainActivity is paused, and eventually
finish()
'd duringrequestPermissions()
. From Android documentation requestPermissionsIf that is the case.. you may do a
finish()
clear specific activity programmatically inside theMainActivity { .. }
to remove it from the activities stack, not in the Manifest.This was in my case !