Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Android deep links are only launched once and don't make the app crash #312

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions androidApp/src/main/java/fr/paug/androidmakers/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.paug.androidmakers

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
Expand All @@ -10,9 +11,8 @@ import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.produceState
import androidx.core.util.Consumer
import androidx.core.view.WindowCompat
import androidx.credentials.ClearCredentialStateRequest
import androidx.credentials.CredentialManager
Expand Down Expand Up @@ -49,6 +49,8 @@ class MainActivity : ComponentActivity() {

logFCMToken()

val initialDeepLink: String? = if (savedInstanceState == null) intent.dataString else null

setContent {
KoinContext {
AndroidMakersTheme {
Expand All @@ -67,16 +69,14 @@ class MainActivity : ComponentActivity() {
onDispose { }
}

var deeplink: String? by remember { mutableStateOf(null) }

intent.data?.let {
deeplink = it.toString()
}

addOnNewIntentListener {
it.data?.let {
deeplink = it.toString()
val deeplink: String? by produceState(initialDeepLink) {
val listener = Consumer<Intent> { newIntent ->
newIntent.dataString?.let {
value = it
}
}
addOnNewIntentListener(listener)
awaitDispose { removeOnNewIntentListener(listener) }
}

MainLayout(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ private fun MainNavHost(
signingCallbacks: SigninCallbacks,
deeplink: String? = null,
) {
LaunchedEffect(deeplink) {
deeplink?.let {
mainNavController.navigate(deeplink)
}
LaunchedEffect(deeplink) {
deeplink?.let {
try {
mainNavController.navigate(deeplink)
} catch (e: IllegalStateException) {
// Invalid route URL
}
}
}

NavHost(
navigator = mainNavController,
Expand Down
Loading