Problems with launch Intent searching via Package manager

Alan Cooke
1 min readJul 19, 2015

--

So it took me the best part of a day of hunting and hair pulling to catch this issue.

As part of some day to day development work and trying to make my colleagues subsequent work easier I wanted to create a Utility class which would provide him with a simplified way of interacting from one app to another. In order todo this we landed on Intent juggling and relying onActivityResult

Here is the code, before the fix:

PackageManager manager = context.getPackageManager();...
intent = manager.getLaunchIntentForPackage(APP_PACKAGE_NAME);
...
return intent;

The problem, onActivityResult was returning before the called Activity had even rendered. Something was up, so I started to hunt in the usual places. Make sure singleTop or singleTask wasn’t set on the calling Activity, no joy…

I eventually decided to dig into some source code and found that the method PackageManager#getLaunchIntentForPackage adds a flag to the Intent it returns, flag in question is: FLAG_ACTIVITY_NEW_TASK

For those of you who are interested can find the Source to the concrete implementation of PackageManager aka ApplicationPackageManager

Fix for the problem, clear the set flags, in our case we didn’t need any set

PackageManager manager = context.getPackageManager();
...
intent = manager.getLaunchIntentForPackage(APP_PACKAGE_NAME);
if (intent != null) {
intent.setFlags(0);
}
...
return intent;

I am writing this up so that it hopefully helps someone else and saves some time and also for my own sanity if ever happens again, I can recall my notes.

--

--

Alan Cooke

Tech Lead Mobile Apps team @ Zendesk and gadget fan!