Deep link into an app while the app is already running in the background
Asked Answered
A

3

12

I have implemented a DeeplinkActivity to catch the intent-filter data scheme and open an activity. The issue I am having is the app is already open in the background and then the user clicks a deep link to open the home screen activity. If the user presses back to get out of the app it will go to what was running in the background. I wanted it to just back out of the app.

I have tried this.

        Intent intent = new Intent(this, LaunchActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent);

But this does not work. Any suggestions?

Aftermath answered 15/9, 2014 at 21:58 Comment(5)
Perhaps FLAG_ACTIVITY_CLEAR_TASK is what you're looking for?Dudek
Ill try that and get back to you tomorrow. Thanks!Aftermath
Doesnt work... Is there some way to bring it to the front if it is running if not open the app?Aftermath
@DDukesterman:- any luck as I am also facing the same issue.Proponent
how did you solve this? I am also facing this issue. If the app open before click the link then navigation not working please adviceDisinfest
J
6

I have just had this problem fixed myself.

First you need to go to the manifest and set launchMode to "singleTask", this will prevent your app from opening a new instance entirely.

Second you need to go the Activity that's accepting that intent filter and override this method

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
}

now by using the new intent here you will have access to the deep link and be able to route to where you need to within your app.

Jihad answered 4/8, 2020 at 9:52 Comment(0)
M
1

Found this almost 2 years old question while facing exactly the same problem... Probably too late, but for anyone other with the same problem: My solution to this is to use instead of current (activity) context the application context. So the 3rd line looks like: getApplicationContext().startActivity(intent);

Matrona answered 1/9, 2016 at 12:29 Comment(1)
Although it works, be aware, to use this solution you'll need the FLAG_ACTIVITY_NEW_TASK in your intent, because getApplicationContext() is not an Activity context.Gilead
Q
0

There are actually two ways to handle your problem:

Either set android:launchMode="singleInstance" (reference) in the manifest of your activity, which is called from the deep link. So, the activity is always the single and only member of its task. So the activity won't reuse any tasks from the backstack in your app which is already running. Be also careful with singleInstance, if you open a singleInstance activity with a deep link and then navigate from there to another activity and you press back, you'll get to the parent activity of the current activity, and not to your singleInstance activity. So, it destroys somehow the standard back navigation and you have to handle all these special cases, which can be quite annoying.

Or, for API >= 16, you can use: finishAffinity() (reference) in your onBackPressed() method of your activity, but here you have to distinguish somehow, if the app was opened via deep link, otherwise it's going to close your app, even when you want just to navigate back to your main menu.

Quart answered 17/3, 2017 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.