Compose Navigation๐Ÿ”ฅ

ํ‘ธ๋ฅธํ•˜๋Š˜ยท2022๋…„ 7์›” 2์ผ
0

์ปดํฌ์ฆˆ

๋ชฉ๋ก ๋ณด๊ธฐ
4/5
post-thumbnail
post-custom-banner

ํœด๋ฆฌ์Šคํ‹ฑ

  • ํ•ฉ๋ฆฌ์ ์ธ ํŒ๋‹จ์ด ๊ตณ์ด ํ•„์š”ํ•˜์ง€ ์•Š์€ ์ƒํ™ฉ์—์„œ ์‚ฌ๋žŒ๋“ค์ด ๋น ๋ฅด๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ณด๋‹ค ์šฉ์ดํ•˜๊ฒŒ ๊ตฌ์„ฑ๋œ ๊ฐ„ํŽธ ์ถ”๋ก ์˜ ๋ฐฉ๋ฒ•

๋‹ค์–‘ํ•œ ํ™”๋ฉด ๋ฐ ์•ฑ ๊ฐ„ ํƒ์ƒ‰์„ ์‰ฝ๊ฒŒ ๊ตฌ์„ฑํ•˜๋ฉฐ ํƒ์ƒ‰ ํœด๋ฆฌ์Šคํ‹ฑ ํŒจํ„ด์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

  • ํƒ์ƒ‰ ๊ทธ๋ž˜ํ”„์—์„œ ๋Œ€์ƒ์„ ํ‘œ์‹œํ•˜๋Š” ๋นˆ ์ปจํ…Œ์ด๋„ˆ์ž…๋‹ˆ๋‹ค.
  • NavHost์—์„œ ์•ฑ ํƒ์ƒ‰์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.
  • Navigation ๊ตฌ์„ฑ์š”์†Œ์˜ ์ค‘์‹ฌ API๋กœ, ์Šคํ…Œ์ดํŠธํ’€(Stateful)์ด๋ฉฐ ์•ฑ์˜ ํ™”๋ฉด๊ณผ ๊ฐ ํ™”๋ฉด ์ƒํƒœ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์ปดํฌ์ €๋ธ”์˜ ๋ฐฑ ์Šคํƒ์„ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค.
val navController = rememberNavController()
  • ์•ฑ์„ ํƒ์ƒ‰ํ•˜๋Š” ๋™์•ˆ ํƒ์ƒ‰ ๊ทธ๋ž˜ํ”„์—์„œ ํŠน์ • ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋ผ ์ด๋™ํ• ์ง€, ํŠน์ • ๋Œ€์ƒ์œผ๋กœ ์ง์ ‘ ์ด๋™ํ• ์ง€ NavController์—๊ฒŒ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ

์‹คํ–‰ํ™”๋ฉด

Build.Gradle ํŒŒ์ผ ์ถ”๊ฐ€

dependencies {
    def nav_version = "2.4.2"

    implementation("androidx.navigation:navigation-compose:$nav_version")
}

Screen (Sealed Class)

  • ์‚ฌ์šฉ์ž๊ฐ€ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์–ด๋–ค ์Šคํฌ๋ฆฐ์„ ์‚ฌ์šฉํ•ด์ค˜์•ผ ํ• ์ง€ ๋ฐฉํ–ฅ์„ ์ •ํ•ด์ค€๋‹ค.
sealed class Screen(val route: String) {
    object Home: Screen("home_screen")
    object Detail: Screen("detail_screen")
}
  • NavHostController ๊ฐ€์ ธ์˜ค๋ฉฐ navController ์— ๋”ฐ๋ผ route๋ฅผ ๋ฐ”๊ฟ”์ค˜์„œ ํ™”๋ฉด์„ ๋‹ค๋ฅด๊ฒŒ ๋ณด์—ฌ์ค€๋‹ค.
@Composable
fun SetUpNavGraph(
    navController: NavHostController
) {
    NavHost(navController = navController,
        startDestination = Screen.Home.route
    ) {
        composable(
            route = Screen.Home.route
        ){
            HomeScreen(navController = navController)
        }
        composable(
            route = Screen.Detail.route
        ){
            DetailScreen(navController = navController)
        }
    }
}

HomeScreen.kt

  • navController ์— ๋”ฐ๋ผ ๋ฐฉํ–ฅ์„ ์ง€์ •ํ•ด์ฃผ๋Š”๋ฐ Modifier.clickable ํ•  ๋•Œ๋งˆ๋‹ค navigate ๋ฐฉํ–ฅ์„ Detail๋กœ ๋„˜๊ฒจ์ค˜์„œ DetailScreen.kt๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
@Composable
fun HomeScreen(
    navController: NavController
) {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Text(
            modifier = Modifier.clickable {
                navController.navigate(route = Screen.Detail.route)
            },
            text = "Home Text", color = Color.Blue, fontSize = 30.sp, fontWeight = FontWeight.Bold
        )

    }
}

DetailScreen.kt

@Composable
fun DetailScreen(
    navController : NavController
) {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {

        Text(modifier = Modifier.clickable {
             navController.navigate(Screen.Home.route){
                 popUpTo(Screen.Home.route){
                     inclusive = true
                 }
             }
//              navController.popBackStack()
//            navController.navigate(route = Screen.Home.route)
        },
            text = "Detail Text",color = Color.Red,fontSize = 30.sp, fontWeight = FontWeight.Bold)
    }
}
  1. basic ํ•œ ํ™”๋ฉด ๋„˜๊ธฐ๋Š” ๋ฐฉ์‹ DetailScreen to HomeScreen
 Text(modifier = Modifier.clickable {
             navController.navigate(Screen.Home.route)
  1. popBackstack() ๋ฐฉ์‹

    HomeScreen -> DetailScreen ์œผ๋กœ Stack๋œ ์ƒํ™ฉ์„ pop ํ•˜๋Š”๋ฐฉ์‹

Text(modifier = Modifier.clickable {
             navController.popBackStack()
  1. popUpTo ๋ฐฉ์‹
    A -> B -> C -> A ์ด์–ด์ ธ ์žˆ์„๋•Œ
  • popUpTo(A)
    • inclusive = False
      ( Go to A Screen ) remain B and C from the stack ํ•œ๋‹ค
    • inclusive = True
      ( Go to A Screen ) remove B and C from the stack ํ•œ๋‹ค

Text(modifier = Modifier.clickable {
             navController.navigate(Screen.Home.route){
                 popUpTo(Screen.Home.route){
                     inclusive = true
                 }
             }

์ฐธ๊ณ ์ž๋ฃŒ

popBackStack / popUpto
์˜์ƒ ์ž๋ฃŒ
GitHub

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€