How to prevent Re-creating an Activity while changing the permissions at runtime in Android M or above
Asked Answered
T

2

10

My app uses location permission. If this permission is revoked at runtime from App Settings and coming back to app, the current activity is destroyed and recreated. How to prevent the recreation of that activity?

Tarantass answered 25/10, 2016 at 12:59 Comment(0)
E
15

You can't. If the user revokes a permission from your app, Android terminates your process. This forces you to go back through your check-for-the-permission code, where you will find out that you lost the permission.

Since your app's process can be terminated at any point when your UI is not in the foreground, your app should already be able to handle this case. If it cannot, then you need to fix that, as your process will be terminated for other reasons while it is in the background (OS freeing up system RAM, user using a task-killer app, user pressing Force Stop in Settings, etc.).

Elodia answered 25/10, 2016 at 13:2 Comment(1)
Awesome, didn't know this behaviour. Thanks!Santossantosdumont
S
1

That is the straightforward cleanup mechanism implemented in android to handle the change in permissions in android M. The process gets terminated and on coming live, init's itself with the available permissions.

So, the answer to your query -> you cant prevent recreation of your activity as the whole process is restarted. At max if you still wish to detect such an event, you could persist some flag before revoking permission and check it after your app comes into foreground.

Swen answered 25/10, 2016 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.