Skip to main content

JetpackComposeにNavigationを入れてみた


JetpackComposeにNavigationを入れました
そのやり方


・navigationの設定
activityとcomposableで飛ばし方が違う
https://stackoverflow.com/questions/72800451/starting-activity-using-compose-navigation

val navController = rememberNavController()
val scope = rememberCoroutineScope()
NavHost(navController = navController, startDestination = "path2") {
    composable("path2") {
        SampleScreen2()
    }
    activity(route = "activity") {
        activityClass = Activity::class
    }
}

画面遷移

navController.navigate("path2")

themeの当て方
共通の部分がどこまでかによって親で囲う範囲が違うので注意

//themeが同じなら
Theme {
    composable
    composable
}
//themeが別なら
composable {
    Theme {

    }
}
composable {
    Theme {

    }
}

 

・パラメーターの渡し方
渡す側

navController.navigate("path/aaaa")

受け取り側

composable("path/parameter") { backStackEntry ->
    SampleScreen(
        query = backStackEntry.arguments?.getString("parameter") ?: "",
    )
}

 

・戻り方
backstackのcomposableへの戻し方
https://engawapg.net/jetpack-compose/1393/screen-transition/#i-5

navController.navigateUp()

関連記事:

Pocket