๐Ÿ”ฅTIL๐Ÿ”ฅJetpack Compose | Navigation

hyihyiยท2024๋…„ 5์›” 14์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
70/70
post-thumbnail

Navigation ๊ณต์‹ ๋ฌธ์„œ

๐Ÿ“– Navigation with Compose

1. Add dependency

dependencies {
    val nav_version = "2.7.7"

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

2. Create a navigation controller

val navController = rememberNavController()

3. Design your navigation graph

NavHost(navController = navController, startDestination = "profile") {
    composable("profile") { Profile( /* ... */ ) }
    composable("friendslist") { FriendsList( /* ... */ ) }
    // Add more destinations similarly.
}

4. Navigate to a destination

ํƒ์ƒ‰ ๊ทธ๋ž˜ํ”„์—์„œ ์ปดํฌ์ €๋ธ”๋กœ ์ด๋™ํ•˜๋ ค๋ฉด NavController.navigate(route)๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
์ด ์˜ค๋ฒ„๋กœ๋“œ๋ฅผ ํ†ตํ•ด navigate()๋Š” ๋‹จ์ผ String ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
์ด๋Š” route์ด๋ฉฐ ๋Œ€์ƒ์˜ ํ‚ค ์—ญํ• ์„ ํ•œ๋‹ค.

navController.navigate("friendslist")

๐Ÿ“ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ

1. NavHost

  • ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ทธ๋ž˜ํ”„๋ฅผ ํ˜ธ์ŠคํŠธ
  • NavController์™€ startDestination์„ ์„ค์ •
  • composable ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํ™”๋ฉด์„ ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ทธ๋ž˜ํ”„์— ์ถ”๊ฐ€

๋„ค๋น„๊ฒŒ์ด์…˜ ๊ทธ๋ž˜ํ”„ : NavHost์™€ Composable ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํ™”๋ฉด๊ณผ ๊ฒฝ๋กœ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ์‹

NavHost(navController = navController, startDestination = "home") {  // NavHost
    composable("home") {  // NavGraph ์ •์˜
        HomeScreen(onNavigateToDetails = { navController.navigate("details") })
    }
    composable("details") {
        DetailScreen(onNavigateToHome = { navController.navigate("home") })
    }
}

2. NavGraph

  • ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ฒฝ๋กœ์™€ ํ™”๋ฉด์˜ ์ •์˜๋ฅผ ํฌํ•จํ•˜๋Š” ๊ทธ๋ž˜ํ”„
  • NavHost ๋‚ด๋ถ€์—์„œ ์ •์˜๋˜๋ฉฐ ๊ฐ composable ํ•จ์ˆ˜๋กœ ํ™”๋ฉด๊ณผ ๊ฒฝ๋กœ๋ฅผ ๋งคํ•‘
composable("home") {  // NavGraph์˜ ์ผ๋ถ€
    HomeScreen(onNavigateToDetails = { navController.navigate("details") })
}
composable("details") {  // NavGraph์˜ ์ผ๋ถ€
    DetailScreen(onNavigateToHome = { navController.navigate("home") })
}

3. NavHostController

  • ๋„ค๋น„๊ฒŒ์ด์…˜์„ ๊ด€๋ฆฌํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋กœ, ์ด๋ฅผ ํ†ตํ•ด ํ™”๋ฉด ๊ฐ„ ์ด๋™์„ ์ฒ˜๋ฆฌ
  • rememberNavController ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ
val navController = rememberNavController()  // NavController ์ƒ์„ฑ

๐Ÿ‘† ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ

์•ˆ๋“œ๋กœ์ด๋“œ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ๊ถŒ์žฅํ•˜๋Š” ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„(UDF)์›์น™์„ ๋”ฐ๋ผ์•ผ ํ•œ๋‹ค.
์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ์ปดํฌ์ €๋ธ”์— NavController๋ฅผ ์ง์ ‘ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ณ , ๋Œ€์‹  ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.

โŒ NavController๋ฅผ ์ง์ ‘ ์ฐธ์กฐํ•˜๋Š” ์˜ˆ์ œ

@Composable
fun MyApp() {
    val navController = rememberNavController()
    NavHost(navController, startDestination = "home") {
        composable("home") { HomeScreen(navController)} // ์ง์ ‘ ์ฐธ์กฐ
        composable("details") { DetailScreen(navController)} // ์ง์ ‘ ์ฐธ์กฐ
    }
}

@Composable
fun HomeScreen(navController: NavHostController) { // ์ง์ ‘ ์ฐธ์กฐ
    Column(modifier = Modifier.fillMaxSize()) {
        Text(text = "Home Screen")
        Button(onClick = { navController.navigate("details")  }) { // ์ง์ ‘ ์ฐธ์กฐ
            Text(text = "Go to Details")
        }
    }
}

@Composable
fun DetailScreen(navController: NavHostController) { // ์ง์ ‘ ์ฐธ์กฐ
    Column(modifier = Modifier.fillMaxSize()) {
        Text(text = "Details Screen")
        Button(onClick = { navController.navigate("home") }) { // ์ง์ ‘ ์ฐธ์กฐ
            Text(text = "Go to Home")
        }
    }
}

โญ• UDF ์›์น™์„ ๋”ฐ๋ฅธ ๋„ค๋น„๊ฒŒ์ด์…˜ ์˜ˆ์ œ

๋„ค๋น„๊ฒŒ์ด์…˜ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋„๋ก ์ˆ˜์ •

@Composable
fun MyApp() {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = "home") {
        composable("home") { 
        	//๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ
            HomeScreen(onNavigateToDetails = { navController.navigate("details")})
        }
        composable("details") { 
        	//๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ
            DetailScreen(onNavigateToHome = { navController.navigate("home")})
        }
    }
}

@Composable
fun HomeScreen(onNavigateToDetails: () -> Unit) {
    Column(modifier = Modifier.fillMaxSize()) {
        Text(text = "Home Screen")
        Button(onClick = onNavigateToDetails) { //๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ๋„ค๋น„๊ฒŒ์ด์…˜ ์ฒ˜๋ฆฌ
            Text(text = "Go to Details")
        }
    }
}

@Composable
fun DetailScreen(onNavigateToHome: () -> Unit) {
    Column(modifier = Modifier.fillMaxSize()) {
        Text(text = "Detail Screen")
        Button(onClick = onNavigateToHome) { //๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด ๋„ค๋น„๊ฒŒ์ด์…˜ ์ฒ˜๋ฆฌ
            Text(text = "Go to Home")
        }
    }
}
  1. MyApp ์ปดํฌ์ €๋ธ”์—๋Š” NavController ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ๋‹ค.
  2. ๋”ฐ๋ผ์„œ navigate() ํ˜ธ์ถœ์€ HomeScreen๊ณผ ๊ฐ™์€ ํ•˜์œ„ ์ปดํฌ์ €๋ธ”์ด ์•„๋‹ˆ๋ผ ์—ฌ๊ธฐ์„œ ๋ฐœ์ƒํ•ด์•ผ ํ•œ๋‹ค.ใ…ฃ
  3. ProfileScreen์—๋Š” ํด๋ฆญ ์‹œ ์‚ฌ์šฉ์ž๋ฅผ DetailScreen์œผ๋กœ ์ด๋™ํ•˜๋Š” ๋ฒ„ํŠผ์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ navigate() ์ž์ฒด๋ฅผ ํ˜ธ์ถœํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค.
  4. ๋Œ€์‹  ๋ฒ„ํŠผ์€ onNavigateToDetails ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋…ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  5. MyApp๊ฐ€ navigation ๊ทธ๋ž˜ํ”„์— HomeScreen๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด onNavigateToDetails ์˜ ๊ฒฝ์šฐ navigate()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋žŒ๋‹ค๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
  6. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ HomeScreen ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋•Œ details๋กœ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ด๋™ํ•œ๋‹ค.

๐Ÿ‘‰์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ปดํฌ์ €๋ธ” ํ•จ์ˆ˜๋Š” NavController๋ฅผ ์ง์ ‘ ์ฐธ์กฐํ•˜์ง€ ์•Š๊ณ , ๋„ค๋น„๊ฒŒ์ด์…˜ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด๋ฅผ ํ†ตํ•ด ์ปดํฌ์ €๋ธ” ํ•จ์ˆ˜์˜ ๋…๋ฆฝ์„ฑ์„ ์œ ์ง€ํ•˜๊ณ , ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„(UDF) ์›์น™์„ ๋”ฐ๋ฅด๊ฒŒ ๋œ๋‹ค.

profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

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