LoginSignup
0
0

More than 1 year has passed since last update.

【Jetpack Compose】値を引き継いで画面遷移する

Posted at

概要

画面遷移をする際に、遷移元の値を遷移先の画面に引き継ぎたいと思ったのですが、少し手こずりました。

コード

test.kt
        composable("favorite") {
            FavoriteScreen (
                toFavorite = {navController.navigate("favorite")},
                toDetail = {productId -> navController.navigate("detail/${productId}")}
            )
        }
        composable(
            "detail/{productId}",
            arguments = listOf(
                navArgument("productId"){
                    type = NavType.IntType
                }
            )
        ) { backStackEntry ->
            val productId = backStackEntry.arguments?.getInt("productId")?:0
            val viewModel:FavoriteScreenViewModel = FavoriteScreenViewModel()
            ProductDetailScreen (productId,viewModel)
        }

内容

Int型の商品番号productIdをFavoriteScreenからProductDetailScreenに渡しています。

注意点

これでひっかかったのは私だけかもしれませんが、引き継ぎ先を指定せずに引き継ぐ値だけ決めてしまうとエラーを吐きます。
どうしてエラーが出てるのかわからなかったので気づくのに時間がかかりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0