Search code examples
androidandroid-permissions

I went to the phone settings, modified a permission, How to make the app restart, back to MainActivity?


I am doing a targetVersion upgrade in my app. Now I have a problem. When I go to phone settings, I manually refused permission. When I go back to my APP, I want to restart it and return to MainActivity. Now when I go back, it just restarts the current Activity. Can you give me some advice?

I checked some programs. They confirmed whether they were modified by judging savedInstanceState != null. I think this is wrong because I backed up the data in savedInstanceState and only judged savedInstanceState != null is not OK, it is one-sided.


Solution

  • You'll need to store your data in more persistent matter (shared preferences, database, files, whatever applies) and manually restore it.

    When user revokes permissions through settings app process gets killed without any callbacks to prevent unwanted behavior, for example:

    1. Activity checks READ permission in onCreate
    2. user pauses activity and revokes permission in settings
    3. Activity resumes assuming it still owns permission and crashes on read attempt

    This includes killing all ongoing services as well. Regular Activity lifecycle doesn't apply in that case.