I have implemented deep linking in my app. I added this intent filter in my manifest file, and the deep linking is working.
just solve this issue for only one Instance
android:launchMode="singleInstance"
<activity
android:name=".SplashScreen"
android:screenOrientation="portrait"
android:launchMode="singleInstance"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="nvd.abc" />
</intent-filter>
</activity>
I solve these issue by just add android:launchMode="singleTask" in manifest file
Well we had several issues with deeplinks. Like:
So the following is not an clear answer to the question more an allround solution for several issues we had.
Our solution:
a) Created a new FragmentActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); //well can be anything some "loading screen"
Intent intent = getIntent();
String intentUrl = intent.getDataString();
Intent newIntent = new Intent(this, MainActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.putExtra("intentUrl",intentUrl);
newIntent.setAction(Long.toString(System.currentTimeMillis()));
startActivity(newIntent);
finish();
}
b) Manifest:
<activity
android:name="YOUR.NEW.FRAGMENT.ACTIVITY"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="scheme1" /> <!-- sheme1://-->
<data android:host="yourdomain.com" />
<data android:host="www.yourdomain.com" />
</intent-filter>
</activity>
c) handle the passed new intent in your activities onCreate() and onResume() by calling the following example function:
private void handleUrl(Intent i){
String intentUrl = null;
if (i != null) {
intentUrl = i.getStringExtra("intentUrl");
if (intentUrl == null){
//hmm intent is damaged somehow
} else {
//because of onResume()
if ( i.getBooleanExtra("used",false) ) {
return;
}
i.putExtra("used", true);
//DO SOMETHING WITH YOUR URL HERE
}
}
the accepted answer didn't work for me, here is what did:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
from the official doc:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
I had this exact same problem, except I wanted the user to land back in the main task with the full back stack, as though they had just used the app switcher to move to my app. To accomplish this I had to reorder tasks.
1) Give my app permission to re-order tasks
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.app">
<uses-permission android:name="android.permission.REORDER_TASKS"/>
</manifest>
2) Keep track of what the main task ID is
public class MainActivity {
public static int mainTaskId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super(savedInstanceState);
//set the main task ID
taskId = getTaskId();
}
}
3) When my deep link activity is launched it saves some data for use later and then brings the main task to the front
public class DeepLinkActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super(savedInstanceState);
//persist deep link data
Uri uri = intent.getData();
String action = intent.getAction();
saveForLater(uri, action);
if(isTaskRoot()){
//I'm in my own task and not the main task
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(MainActivity.taskId, ActivityManager.MOVE_TASK_NO_USER_ACTION);
}
}
}
}
4) When whatever activity is at the top of the main task's back stack starts, it checks if there's any saved data to work on, and works on it.
(initialize at the starting of class)
String itemInfo == "";
Basically compare the package name.
if(!itemInfo.equals(getItem(position).activityInfo.packageName))
{
intent.setComponent(new ComponentName(getItem(position).activityInfo.packageName,
getItem(position).activityInfo.name));
itemInfo = getItem(position).activityInfo.packageName;
((AxisUpiActivtiy) context).startActivityForResult(intent, RequestCodes.START_INTENT_RESPONSE);
}
this condition itemInfo.equals(getItem(position).activityInfo.packageName)
is the important