Last active
October 18, 2017 05:44
-
-
Save jie-meng/9ba37eaa362a63b03eba4b68a8aca99e to your computer and use it in GitHub Desktop.
Fix Android App restarts rather than resumes when launch from launcher bug.
This is usually happen when you install an App from mobile device, package manager installs it then you would click 'OPEN', After App startup, press home button and then launch App from launcher. Then the app would brought to front and create a new MainActivity, that was…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The behavior you are experiencing is caused by an issue that exists in some Android launchers since API 1. You can find details about the bug as well as possible solutions here: https://code.google.com/p/android/issues/detail?id=2373. | |
//It's a relatively common issue on Samsung devices as well as other manufacturers that use a custom launcher/skin. I haven't seen the issue occur on a stock Android launcher. | |
//Basically, the app is not actually restarting completely, but your launch Activity is being started and added to the top of the Activity stack when the app is being resumed by the launcher. You can confirm this is the case by clicking the back button when you resume the app and are shown the launch Activity. You should then be brought to the Activity that you expected to be shown when you resumed the app. | |
//The workaround I chose to implement to resolve this issue is to check for the Intent.CATEGORY_LAUNCHER category and Intent.ACTION_MAIN action in the intent that starts the initial Activity. If those two flags are present and the Activity is not at the root of the task (meaning the app was already running), then I call finish() on the initial Activity. That exact solution may not work for you, but something similar should. | |
//Here is what I do in onCreate() of the initial/launch Activity: | |
if (!isTaskRoot() | |
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER) | |
&& getIntent().getAction() != null | |
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) { | |
finish(); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment