Android and the mystery of the disappearing launcher icon.
Working in the mobile eco system we constantly see weird behaviors, sometimes its easier to classify them as an edge case or glitch in the system, and with half a smile ask our testers to come back to us when they reproduce it.
This is a story about one of those issues, that turned to be not a glitch (like they always do)
Few years ago, I had the luck to work in a great company with huge user base app (100+ million downloads) and as we all know:
At the time, we had a QA team of 5–8 testers constantly testing the product. The team also put a lot of effort into testing app updates. They installed previous version added some data to it and then updated to the new one. All to make sure the data is shown properly. And when more then one tester came to you with a weird issue you know it will be a problem.
The issue
The weird issue that they saw, was that after installing the app update, the app icon they have added to their launcher as shortcut disappeared.
I encountered the same issue on my current project and decided to share my experience with it.
Lots of sweat went into google searches to solve this one, it appears that when you add a shortcut to your app it links to a specific Activity and not to your app itself (since you can have more then on launcher per application, who does that anyway?). It means just changing the name of the launcher activity or its package will cause the shortcut to disappear.
Example:
Moving from:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
To:
<activity android:name=".OnBoardingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Will make your icon disappear.
This usually will happen to you when:
- You refactor some old code by changing activity name
- Add on-boarding / splash screens
- Change navigation scheme
The solution
To avoid it, I always start a new project by creating a StartUpActivity to be the project launcher. In its on create I will decide what Activity I should open (On boarding? Main activity?) and call finish() in the onCreate() method.
Also make sure you’re not calling setContentView(), you don’t want to render your StartUpActivity.
If you like most developers you’re not starting on a new product you have at lest 2 ways to solve it:
- Do not delete your previous Activity completely, empty it and call the right one from it. (DON’T DO IT)
- Use <activity-alias /> in manifest to link your previous name to the new Activity like this:
<activity-alias android:name="[PREVIOUS_ACTIVITY_PATH]"
android:targetActivity="[NEW_ACTIVITY_PATH]">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
Note: activity-alias tag must be declared after you declare your new Activity in the manifest.
So actually you have only one good and mostly clean way to preserve you launcher shortcut icon, do it with: activity-alias.