๐ก Navigation Compose๋ Compose ๋ฐฐํฌ ๋ฒ์ ๊ณผ ๋์ผํ๊ฒ ์ ๋ฐ์ดํธ ๋์ง ์์ต๋๋ค.
๋๋ฌธ์, https://developer.android.com/jetpack/compose/navigation?hl=ko ์์ Navigation ๋ฒ์ ์ ํ์ธํ์๊ณ ์ค์ ์ ์งํํด์ผ ํฉ๋๋ค.
dependencies {
val nav_version = "2.5.3"
implementation("androidx.navigation:navigation-compose:$nav_version")
}
dependencies {
implementation("androidx.navigation:navigation-compose:2.5.3")
}
popUpTo
// room1์ผ๋ก ์ด๋ํ๊ธฐ ์ home์ ์ ์ธํ ๋๋จธ์ง ์คํ์ ๋ชจ๋ ์ ๊ฑฐ ํ room1์ผ๋ก ์ด๋
navController.navigate("room1") {
popUpTo("home")
}
Home, Room1, Room2๊ฐ ์๋ค๊ณ ๊ฐ์
์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ํ์ผ๋ก ์ด๋์ popUpTo
๋ฅผ ์ง์
popUpTo
๋ Home โ Room1
์ผ๋ก ์ด๋ํ Room1 โ Room2
๋ก ์ด๋ํ๋ฉด Room1
์ ์ฌ๋ผ์ง
(์ํ Home โ Room2
)
์ฆ, Home๊ณผ ์ด๋ํ๋ ค๋ ๊ณณ ์ฌ์ด๋ฅผ ์ ๊ฑฐํ๋ค.
inclusive
navController.navigate("Home") {
popUpTo("Home") {
inclusive = true
}
}
inclusive = ture
๋ฅผ ์ฌ์ฉํ๋ฉด Home๊น์ง ์ ๊ฑฐ๋ฅผ ํ๋ค.Home โ Room1
์ผ๋ก ์ด๋์ Home์ด ์ ๊ฑฐ๋๋ค. (์ํ : Room1) Room1 โ Room2
๋ก ์ด๋์์๋ popUpto๊ฐ ๋์ํ์ง ์๋๋ค. (์ํ Room1 โ Room2
)Room1 โ Room2 โ Home
์ผ๋ก ์์ Home์ด ์์ด ์๋ฌด๊ฒ๋ ์ ๊ฑฐ๋์ง ์๋๋ค. Room1 โ Room2 โ Room1
์ผ๋ก Home์ด ์ ๊ฑฐ๋๋ค.launchSingleTop
launchSingleTop
: ์คํ ์ต์๋จ์ ์ฌ๋ผ๊ฐ ์์ผ๋ฉด, ์คํ์ ์์ง ์์navController.navigate("Home") {
launchSingleTop = true
}
launchSingleTop
์ ์ฌ์ฉํ์ง ์์ผ๋ฉด Home์์ Home์ผ๋ก ์ด๋ํ๋ ๋ฒํผ์ ํด๋ฆญ์ ์คํ์ Home์ด ์ฌ๋ฌ๊ฐ ์์launchSingleTop
์ ์ฌ์ฉํ๋ฉด Home์ ์ฌ๋ฌ๋ฒ ๋๋ฌ๋ ์ด๋ฏธ Home์ด ์๊ธฐ ๋๋ฌธ์ ๋์ด์ ์์ด์ง ์์Argument/dong
์ ์ ๋ฌ@Composable
fun Navigation(
modifier: Modifier = Modifier,
navController: NavHostController = rememberNavController(),
) {
// NavHost ์์ฑ
// ์ปจํธ๋กค๋ฌ, ๋ชฉ์ ์ง, modifier
NavHost(navController, "Home", modifier = modifier) {
// ๋ผ์ฐํฐ ์ค์
composable("Home") {
Column {
// argument nav
Button(onClick = {
navController.navigate("Argument/dong") {
launchSingleTop = true
}
}) {
Text("userId ์
๋ ฅ๊ฐ ์ผ๋ก ์ฐ๊ฒฐ ์ด๋")
}
}
}
composable("Argument/{userId}") {
val userId = it.arguments?.get("userId")
Text(text = "userId: $userId")
}
}
}
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.compose_example.ui.theme.Compose_exampleTheme
class NavActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Compose_exampleTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background,
) {
Navigation()
// Greeting("Android")
}
}
}
}
}
/**
* Navigation ์ด๋์ stack์ด ์์ธ๋ค.
*/
@Composable
fun Navigation(
modifier: Modifier = Modifier,
navController: NavHostController = rememberNavController(),
) {
// NavHost ์์ฑ
// ์ปจํธ๋กค๋ฌ, ๋ชฉ์ ์ง, modifier
NavHost(navController, "Home", modifier = modifier) {
// ๋ผ์ฐํฐ ์ค์
composable("Home") {
Column {
Text(text = "Home")
Button(onClick = {
navController.navigate("room1")
}) {
Text("room1 ์ด๋")
}
Button(onClick = {
navController.navigate("room2")
}) {
Text("room2 ์ด๋")
}
Button(onClick = {
navController.navigate("Home") {
launchSingleTop = true
}
}) {
Text("Home์ผ๋ก ์ด๋")
}
// argument nav
Button(onClick = {
navController.navigate("Argument/dong") {
launchSingleTop = true
}
}) {
Text("userId ์
๋ ฅ๊ฐ ์ผ๋ก ์ฐ๊ฒฐ ์ด๋")
}
}
}
composable("room1") {
Column {
Text(text = "room1")
Button(onClick = {
navController.navigate("Home")
}) {
Text("Home์ผ๋ก ์ด๋")
}
Button(onClick = {
navController.navigate("room2")
}) {
Text("room2 ์ด๋")
}
}
}
composable("room2") {
Column {
Text(text = "room2")
Button(onClick = {
navController.navigate("Home")
}) {
Text("Home์ผ๋ก ์ด๋")
}
Button(onClick = {
navController.navigate("room1")
}) {
Text("room1 ์ด๋")
}
}
}
composable("Argument/{userId}") {
val userId = it.arguments?.get("userId")
Text(text = "userId: $userId")
}
}
}