ํด๋ฆฌ์คํฑ
- ํฉ๋ฆฌ์ ์ธ ํ๋จ์ด ๊ตณ์ด ํ์ํ์ง ์์ ์ํฉ์์ ์ฌ๋๋ค์ด ๋น ๋ฅด๊ฒ ์ฌ์ฉํ ์ ์๊ฒ ๋ณด๋ค ์ฉ์ดํ๊ฒ ๊ตฌ์ฑ๋ ๊ฐํธ ์ถ๋ก ์ ๋ฐฉ๋ฒ
๋ค์ํ ํ๋ฉด ๋ฐ ์ฑ ๊ฐ ํ์์ ์ฝ๊ฒ ๊ตฌ์ฑํ๋ฉฐ ํ์ ํด๋ฆฌ์คํฑ ํจํด์ ์ ์ฉํ ์ ์์ต๋๋ค
val navController = rememberNavController()
dependencies {
def nav_version = "2.4.2"
implementation("androidx.navigation:navigation-compose:$nav_version")
}
sealed class Screen(val route: String) {
object Home: Screen("home_screen")
object Detail: Screen("detail_screen")
}
@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)
}
}
}
@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
)
}
}
@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)
}
}
Text(modifier = Modifier.clickable {
navController.navigate(Screen.Home.route)
HomeScreen -> DetailScreen ์ผ๋ก Stack๋ ์ํฉ์ pop ํ๋๋ฐฉ์
Text(modifier = Modifier.clickable {
navController.popBackStack()
Text(modifier = Modifier.clickable {
navController.navigate(Screen.Home.route){
popUpTo(Screen.Home.route){
inclusive = true
}
}
์ฐธ๊ณ ์๋ฃ