Window manager BadTokenException / WindowLeaked #2208
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
One Line Summary
Fix the BadTokenException and WindowLeaked exception caused by showing a dialog on a finishing or destroyed activity.
Details
Motivation
The exception exists since User Model and now becomes the most occurring exception according to the data from Google Play SDK Console. This exception is relatively less impactful to user experience as it normal happens during app closing or backgrounding phrase, but we want to reduce the amount of reported exception as it may potentially affect app's stability rating over the Play Store.
Scope
The call to show a permission dialog has no effect if the activity is finishing, backgrounded, or destroyed, and it will not be retried unless user is explicitly asking to do so. There is no behavior change because the current version would just crash and will not retrying showing the dialog upon app restart.
OPTIONAL - Other
This PR contains 3 commits: the first commit simulates calling a permission dialog on a finished activity; the second commit shows that the fix is effective in removing the exception in such activity; the third commit cleans up the simulation code and leave the fix code change only.
Related issue: #2014
Testing
Manual testing
By checking out the first commit, I am able to reproduce a WindowLeaked exception by running the sample app.
After the second commit, the exception is no longer reproducible.
BadTokenException can be caused by showing the dialog in a "finishing" activity but I am unable to simulate the situation other than a "finished" activity. Since this fix checks for both states of the activity, I believe this can fixes both BadTokenException and WindowLeaked exceptions.
Affected code checklist
Checklist
Overview
Testing
Final pass
This change is