I have a very simple single activity compose app utilising compose navigation:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=";
xmlns:tools=";>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.MyApplication">
<intent-filter>
<data android:scheme="myscheme" android:host="app" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
Log.d("Blah", "onCreate!!!")
setContent {
MyApplicationTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = "screen-1"
) {
composable(
"screen-1",
deepLinks = listOf(
navDeepLink {
uriPattern = "myscheme://app/screen-1"
action = Intent.ACTION_VIEW
}
)
) {
Text("Screen 1", Modifier.padding(innerPadding))
}
composable(
"screen-2",
deepLinks = listOf(
navDeepLink {
uriPattern = "myscheme://app/screen-2"
action = Intent.ACTION_VIEW
}
)
) {
Text("Screen 2", Modifier.padding(innerPadding))
}
}
}
}
}
}
}
When I trigger a deep link (via the command line or browser) I get duplicate instances of the activity (the log message in onCreate appears twice). This even happens when the deep link triggers a fresh launch of the app (app is not already present in the background). I am using the latest versions of everything. I have tried changing the launch mode and task affinity to no avail. Any ideas?
Thanks!
I have a very simple single activity compose app utilising compose navigation:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.MyApplication">
<intent-filter>
<data android:scheme="myscheme" android:host="app" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
Log.d("Blah", "onCreate!!!")
setContent {
MyApplicationTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = "screen-1"
) {
composable(
"screen-1",
deepLinks = listOf(
navDeepLink {
uriPattern = "myscheme://app/screen-1"
action = Intent.ACTION_VIEW
}
)
) {
Text("Screen 1", Modifier.padding(innerPadding))
}
composable(
"screen-2",
deepLinks = listOf(
navDeepLink {
uriPattern = "myscheme://app/screen-2"
action = Intent.ACTION_VIEW
}
)
) {
Text("Screen 2", Modifier.padding(innerPadding))
}
}
}
}
}
}
}
When I trigger a deep link (via the command line or browser) I get duplicate instances of the activity (the log message in onCreate appears twice). This even happens when the deep link triggers a fresh launch of the app (app is not already present in the background). I am using the latest versions of everything. I have tried changing the launch mode and task affinity to no avail. Any ideas?
Thanks!
You have to define launchMode for MainActivity
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop "
android:theme="@style/Theme.MyApplication">
You can use either "singleTask" or "singleTop" depending on your specific requirements. Here's cases of when to use each:
Use singleTop if you need to preserve the back stack and avoid clearing it.
This is useful when you want to ensure that only one instance of the activity exists at the top of the back stack, but you don't want to clear the rest of the stack.
Use singleTask if you need to guarantee that the activity will be the only one in its task and clear the back stack.
Here is a documentation about start mode documentation about start mode
android:launchMode="singleTask"
for the <activity>? – Yurii Commented Jan 31 at 12:26public override fun onNewIntent(intent: Intent) {
and handle the deeplink from Intent. – Yurii Commented Jan 31 at 12:41