I am currently have two seperate applications, both perform seperate tasks, but there is on limited occasion times when I need one application to use the other if its there.
So I use a function to check the required application exists:
public static boolean isIntentAvailable(Context context, String action)
{
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
If it does, then I use the following to start the activity with an extra on there:
if (isIntentAvailable(ListPOI.this, "com.me.myapp.MY_MAP"))
{
Intent i = new Intent("com.me.myapp.MY_MAP");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("place", true);
startActivity(i);
}
The setFlags means if the user presses home, and they go back to the first app, it opens, it doesn’t open the second app called here.
This all works fine, the first time. However after calling the this the second time, the second app resumes, so it doesn’t pick up the ‘extra’ which has been passed, how can I ensure I get the extra?
The final answer to my question has been using the following:
I created a function to handle loading intent extras:
In onCreate I use:
Then added the following function to get the intent when an existing instance of an activity is passed a new intent:
I also added
android:launchMode="singleInstance"to the activity declaration in the manifest of the activity with the above code.Finally from the first package, I can now use the following code to start the second package with an extra. When the second package starts, the user can click on the first package (from the home launcher) and get the app they expect, and if they click to start the second package, the ‘already running’ instance is shown, but captures the new extra which has been passed:
Hopefully this will be helpfull to someone – its probably not the best way to do this, but it works for me.