Deep link to the app when the app is already running in the background

I applied DeeplinkActivity to catch the intent filter data schema and open the action. The problem I am facing is that the application is already open in the background, and then the user clicks a deep link to open the activity on the main screen. If the user clicks to exit the application, he will go to what was in the background. I wanted it to just return from the application.

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 that does not work. Any suggestions?

+7
android deep-linking
source share
2 answers

There are actually two ways to solve your problem:

Or set android:launchMode="singleInstance" ( link ) to the manifest of your activity, which is called from a deep link. Thus, an activity is always the only and only member of its task. This way, the action will not reuse any tasks from the back of your application that is already running. Also, be careful with singleInstance if you open the SingleInstance operation with a deep link, and then move from there to another action, and you click back, you get into the parent activity of the current activity, and not your singleInstance activity. Thus, it somehow destroys the standard reverse 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 your activity, but here you must somehow distinguish if the application was opened through a deep link, otherwise it will close your application, even if you just want to return to the main menu.

0
source share

Found this almost 2-year-old question, having encountered the same problem ... Probably too late, but for someone else with the same problem: My solution is to use the application context instead of the application context (activity) . So, the 3rd line looks like this: getApplicationContext().startActivity(intent);

-one
source share

All Articles