📱
[Android] JetPack Compose Type SafeなNaigation
※Kotlin Serializationプラグインを入れている必要がある
公式を見てまとめつつDeeplLinkの実装も含めてみた
sealed class Route {
@Serializable
data object Home : Route()
@Serializable
data class ComicDetail(val id: String) : Route() {
companion object {
const val DEEP_LINK_URI_PATTERN = "myapp://comic/{id}"
}
}
}
NavHost(navController, startDestination = Home) {
composable<Home> {
HomeScreen(onNavigateToComicDetail = { id ->
navController.navigate(Route.ComicDetail(id))
})
}
composable<ComicDetail>(
deeplinks = listOf(
navDeepLink { uriPattern = Route.ComicDetail.DEEP_LINK_URI_PATTERN }
)
){ backStackEntry ->
val route = backStackEntry.toRoute<Route.ComicDetail>()
ComicDetail(route.id)
}
}
AndroidManifestにDeepLink用の設定を追加
<activity
android:name=".MainActivity"
android:exported="true">
<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="myapp"
android:host="comic" />
</intent-filter>
</activity>
テスト方法
adb shell am start -W -a android.intent.action.VIEW -d "myapp://comic/123" your.package.name
Discussion