My app has a modal navigation drawer to navigate between home page and target page.
I tried to launch the app and navigate to the target page by tapping a notification containing a deeplink intent, and it actually worked. But when I pressed the home page button in the drawer, navigation didn't show me the home screen but stayed at the target screen.
Then I pressed the system back button, the screen turned to home page, and navigation works as I expected again, it can navigate between two pages.
I deleted the restoreState = true in the drawer navigation button, then I found the navigation works normally, but I couldn't restore the screen state.
So I wonder what caused this issue, did I created an independent activity at the top of the original activity? Or the restoreState restored the entire screen?
Here is my code:
AndroidManifest.xml
<activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:launchMode="singleTop" android:theme="@style/Theme.MyApp" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </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:host="home_page" android:scheme="myapp" /> </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:host="target_page" android:scheme="myapp" /> </intent-filter> </activity> Code to send a notification:
//screenRoute is "home_page" or "target_page" val deeplinkUri = "myapp://$screenRoute".toUri() val deepLinkIntent = Intent( Intent.ACTION_VIEW, deeplinkUri, ).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP } // Create PendingIntent val pendingIntent: PendingIntent = TaskStackBuilder.create(context).run { addNextIntentWithParentStack(deepLinkIntent) getPendingIntent( hashcode, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) } val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val builder = NotificationCompat.Builder(context, NotificationUtils.ALARM_CHANNEL_ID) .setContentTitle("Notification Title") .setSmallIcon(R.drawable.app_icon) .setContentText(message) .setPriority(NotificationCompat.PRIORITY_HIGH) .setContentIntent(pendingIntent) .setAutoCancel(true) notificationManager.notify(1, builder.build()) Code of navigation drawer: I want to set these two pages as top-level pages, so I used popUpTo() to clear the backstack.
DrawerNavigationButton( //....// onClick = { navController.navigate("home_page") { popUpTo(navController.graph.findStartDestination().id) { saveState = true } restoreState = true launchSingleTop = true } } ) DrawerNavigationButton( //....// onClick = { navController.navigate("target_page") { popUpTo(navController.graph.findStartDestination().id) { saveState = true } restoreState = true launchSingleTop = true } } ) And code about nav host:
val uri = "myapp:/" NavHost( navController = navController, startDestination = "home_page" ) { //Home Page composable( route = "home_page", deepLinks = listOf( navDeepLink { uriPattern = "$uri/home_page" } ), ) { backStackEntry -> HomeScreen( //........// ) } //Target Page composable( route = "target_page", deepLinks = listOf( navDeepLink { uriPattern = "$uri/target_page" } ), ) { backStackEntry -> TargetScreen( //........// ) } } Any help would be greatly appreciated!