Custom Action Intent Opens my App in the same context as the app that I clicked From

android, android-intent

Solution

Make your URL look like this:

`intent:#Intent;launchFlags=0x10000000;component=com.mycompany.myapp/com.mycompany.myapp.MyActivity;end`

This URL contains the launchFlag for `Intent.FLAG_ACTIVIY_NEW_TASK`, so this will launch your app in a separate task (outside of the email client or browser or whatever).

EDIT: Add additional details based on OP's comment

You say that you are using a URL like this: http://com.my.app/5058749

In that case you must have used an Intent filter to get Android to open your app by specifying an `<intent-filter>` on a certain `<activity>` in your manifest. There are several things you can do to deal with the problem of the launched Activity ending up in the same task as the Activity that launched it:

1) If the Activity is always intended to be the root (starting, first) Activity of a task, you can put the following code in `onCreate()` after the call to `super.onCreate()`:

if (!isTaskRoot()) {
    // Activity was launched into another task. Restart it in its own task
    Intent intent = new Intent(this, this.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    finish();
    return;
}

2) You can set the launch mode of this Activity to `singleTask` in the manifest by adding

android:launchMode="singleTask"

to the `<activity>` definition. This will cause the Activity to be launched in its own task but this launch mode has other consequences that are more subtle and so you need to be careful about using it. In general I don't like to suggest this because it tends to create more problems than it solves.

3) You can determine if your app was launched from the browser or email client by examining the `Intent` used to start it in `onCreate()` (The Intent will have the `data` set to the URL when launched via the browser or email client). You can then decide if you want to restart it in its own task by using the code I've supplied in option 1 above.

Problem

I have searched a lot on this. But, I have my code working to open my app using a custom intent URL. This URL is usually delivered via email. When I click on the link, it opens my app fine and everything seems to be working; however, it opens in the context of the email application. For example, if I click on the link from Gmail, when I open multitasking, I have to click on Gmail to return back to the app that just opened. I would think it should open my app and I can continue using Gmail while my other app is running. Any thoughts on this?

Original source