티스토리 뷰
//백스택에 home 라우트 직전까지 쌓인것들을 다 지운다 그 다음에 friendslist를 백스택에 쌓는다
// home|friendslist 이런모양이됨
// freindslist에서 뒤로가기 하면 home으로 이동
navController.navigate("friendslist") {
popUpTo("home")
}
// home까지 포함해서 스택에 쌓인것을을 지운다 그다음 friendslist를 백스택에 넣는다
// 그러면 이 경우 friendslist에서 뒤로가기하면 바로 앱이 종료
navController.navigate("friendslist") {
popUpTo("home") { inclusive = true }
}
// 백스택의 최상단에 search가 이미 있다면 걔를 재사용함, 새로 안만든다
navController.navigate("search") {
launchSingleTop = true
}
fun MainBottomNavigationBar(navController: NavHostController) {
val bottomNavigationItems = listOf(
BottomNav.Home,
BottomNav.Setting
)
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
NavigationBar(
containerColor = HabitPurpleNormal,
tonalElevation = 0.dp,
modifier =
Modifier
.graphicsLayer {
shape = RoundedCornerShape(
topStart = 16.dp,
topEnd = 16.dp
)
clip = true
shadowElevation = 20f
}
) {
bottomNavigationItems.forEach { item ->
Spacer(modifier = Modifier.size(4.dp))
NavigationBarItem(
colors = NavigationBarItemDefaults.colors(
selectedIconColor = HabitWhite,
selectedTextColor = HabitWhite,
unselectedIconColor = HabitBlueGray,
unselectedTextColor = Color.Transparent,
indicatorColor = HabitPurpleNormal
),
label = {
Text(text = item.title)
},
icon = { Icon(painter = painterResource(id = item.icon), item.title) },
selected = currentRoute == item.route, //언제 selected 상태가 되는지
onClick = { //네비게이션 아이템 클릭시
navController.navigate(item.route){
if(currentRoute.toString() != item.route){ // 가장 최근 화면이 이동 목적지와 다른 경우에만
popUpTo(currentRoute.toString()){ inclusive = true } // 스택에 있는 가장 최근 화면 제거
}else{ // 가장 최근 화면이 이동 목적지와 동일한경우 새로 쌓지 않고 재사용
launchSingleTop = true
}
}
})
}
}
}
'공부 > Android' 카테고리의 다른 글
[Compose] 로그인 화면(바텀네비게이션 없어야되는화면)과 바텀 네비게이션 있어야 하는 화면 간 처리 (0) | 2023.09.22 |
---|---|
[Compose] 두 Text 기준선 맞추기 (0) | 2023.09.16 |
[Compose] 패딩, 배경색 순서 이슈 ^.^; (0) | 2023.09.11 |
[Wear] Health Service 정리 (0) | 2023.08.22 |
[Watch] 헬스 데이터 사용법 조사.. (1) | 2023.08.20 |