I have a problem in Jetpack Compose: the activity keeps calling onStart -> onStop -> onDestroy methods after I clicked on the deepLink navigation notification, even though I reached the ChatScreen target
My gradle: implementation "androidx.navigation:navigation-compose:2.7.7"
Manifest
<activity
android:name=".presentation.ui.main_screens_activity.ui.MainScreensActivity"
android:exported="false"
android:label="@string/title_activity_main_screens"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize"
android:theme="@style/Theme.Chatalyze"
tools:ignore="AppLinkUrlError"
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="scheme_chatalyze" />
<data android:host="call_screen" />
<data android:scheme="app" />
<data android:host="calls_screen" />
<data android:host="chat_screen" />
</intent-filter>
<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="scheme_chatalyze2" />
<data android:host="chat_screen2" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
Notification class
private fun getPendingIntent(
name: String?,
sender: String,
recipient: String,
context: Context,
): PendingIntent {
val deepLink =
Uri.parse("scheme_chatalyze2://chat_screen2/{$name}/{$sender}/{$recipient}")
val intent = Intent(
Intent.ACTION_VIEW,
deepLink
)
return PendingIntent.getActivity(
context.applicationContext, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}
NavigationGraph
@RequiresApi(Build.VERSION_CODES.S)
@Composable
fun MainScreensNavigationGraph(navHostController: NavHostController) {
Log.d("4444", " ChatalyzeNavigationGraph loaded")
NavHost(navController = navHostController, startDestination = ScreenRoute.ChatsScreen.route) {
composable(route = ScreenRoute.ChatsScreen.route) {
ChatsScreen(navController = navHostController)
}
composable(
route = ScreenRoute.ChatScreen.route,
deepLinks = listOf(
navDeepLink {
// uriPattern = "scheme_chatalyze2://chat_screen2/{$RECIPIENT_NAME}/{$RECIPIENT_PHONE}/{$SENDER_PHONE}"
uriPattern =
"scheme_chatalyze2://chat_screen2/{$RECIPIENT_NAME}/{$RECIPIENT_PHONE}/{$SENDER_PHONE}"
action = Intent.ACTION_VIEW
}
),
arguments = listOf(
navArgument(RECIPIENT_NAME) {
type = NavType.StringType
},
navArgument(RECIPIENT_PHONE) {
type = NavType.StringType
},
navArgument(SENDER_PHONE) {
type = NavType.StringType
}
)
) { entry ->
val recipientName = entry.arguments?.getString(RECIPIENT_NAME)
val recipientPhone = entry.arguments?.getString(RECIPIENT_PHONE)
val senderPhone = entry.arguments?.getString(SENDER_PHONE)
IfLetHelper.execute(recipientName, recipientPhone, senderPhone) {
ChatScreen(
navHostController = navHostController,
recipientName = it[0],
recipientPhone = it[1],
senderPhone = it[2]
)
}
}
}
}
Target Screen
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ChatScreen(
viewModel: ChatScreenViewModel = hiltViewModel(),
navHostController: NavHostController,
recipientName: String,
recipientPhone: String,
senderPhone: String,
) {
Log.d("4444", " ChatScreen loaded")
}
I also tried doing fun getPendingIntent() differently, but it still ends up onDestroy for the MainScreenActivity screen, although I achieved the ChatScreen goal
private fun getPendingIntent(
name: String?,
sender: String,
recipient: String,
context: Context
): PendingIntent {
val deepLink = Uri.parse("scheme_chatalyze2://chat_screen2/{$name}/{$sender}/{$recipient}")
val deepLinkIntent = Intent(
Intent.ACTION_VIEW,
deepLink,
// context,
// MainScreensActivity::class.java
)
val resultPendingIntent: PendingIntent = TaskStackBuilder.create(context).run {
addNextIntentWithParentStack(deepLinkIntent)
getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE)
}
return resultPendingIntent
At the very least, have the activity recreated but only stop at onStart. I've tried using all launch mode types, but the behavior is the same.
I also tried using onNewIntent() in MainScreenActivity and calling deepLink inside, but it still ends up with onDestroy for the MainScreenActivity screen, although I achieved the ChatScreen goal