Search code examples
androidandroid-jetpack-composedeep-linkingandroid-deep-linkjetpack-compose-navigation

Handling deep links in compose SingleActivity app without multiple app instances and ugly animation


I have the SingleActivity app developed with Jetpack Compose and compose navigation.

I need to handle deep links with query parameters.

At the moment my implementation includes the definition of deep links in the NavHost and this is all.

But I have few issues with this approach:

  • ugly animation with blinking after the splash screen when I try to navigate to the app by clicking on the deep link
  • if app has already opened and I click on the link - I have 2 instances of the app in the recents

How can I solve these issues?


Solution

  • I solved it by changing launchMode of the MainActivity to singleTask. Previously it was a default standard mode:

    android:launchMode="singleTask"
    

    And adding handling deep link by NavHostController in the setContent function inside the onCreate method of MainActivity:

    @AndroidEntryPoint
    class MainActivity : FragmentActivity() {
        private val viewModel: AppViewModel by viewModels()
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            actionBar?.hide()
            WindowCompat.setDecorFitsSystemWindows(window, false)
    
            setContent {
                val appState = rememberMyAppState()
    
                // add this
                DisposableEffect(Unit) {
                    val listener = Consumer<Intent> {
                        appState.value.navController.handleDeepLink(it)
                    }
                    addOnNewIntentListener(listener)
                    onDispose { removeOnNewIntentListener(listener) }
                }
             ...
             }
    }
    

    Also to fix blinking between splash and other app content I changed containerColor and contentColor of my main Scaffold