Navigation Compose와 공유된 요소
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
navigation-compose
종속 항목과 함께 공유 요소를 사용하려면 AnimatedVisibilityScope
를 매개변수로 사용하는 Modifier.sharedElement()
를 사용하세요. 이를 사용하여 표시할 항목과 시기를 결정할 수 있습니다.
그림 1. 공유 요소가 있는 탐색 Compose
다음은 공유 요소와 함께 navigation-compose
를 사용하는 예입니다.
@Preview
@Composable
fun SharedElement_PredictiveBack() {
SharedTransitionLayout {
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = "home"
) {
composable("home") {
HomeScreen(
this@SharedTransitionLayout,
this@composable,
{ navController.navigate("details/$it") }
)
}
composable(
"details/{item}",
arguments = listOf(navArgument("item") { type = NavType.IntType })
) { backStackEntry ->
val id = backStackEntry.arguments?.getInt("item")
val snack = listSnacks[id!!]
DetailsScreen(
id,
snack,
this@SharedTransitionLayout,
this@composable,
{
navController.navigate("home")
}
)
}
}
}
}
@Composable
fun DetailsScreen(
id: Int,
snack: Snack,
sharedTransitionScope: SharedTransitionScope,
animatedContentScope: AnimatedContentScope,
onBackPressed: () -> Unit
) {
with(sharedTransitionScope) {
Column(
Modifier
.fillMaxSize()
.clickable {
onBackPressed()
}
) {
Image(
painterResource(id = snack.image),
contentDescription = snack.description,
contentScale = ContentScale.Crop,
modifier = Modifier
.sharedElement(
sharedTransitionScope.rememberSharedContentState(key = "image-$id"),
animatedVisibilityScope = animatedContentScope
)
.aspectRatio(1f)
.fillMaxWidth()
)
Text(
snack.name, fontSize = 18.sp,
modifier =
Modifier
.sharedElement(
sharedTransitionScope.rememberSharedContentState(key = "text-$id"),
animatedVisibilityScope = animatedContentScope
)
.fillMaxWidth()
)
}
}
}
@Composable
fun HomeScreen(
sharedTransitionScope: SharedTransitionScope,
animatedContentScope: AnimatedContentScope,
onItemClick: (Int) -> Unit,
) {
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
itemsIndexed(listSnacks) { index, item ->
Row(
Modifier.clickable {
onItemClick(index)
}
) {
Spacer(modifier = Modifier.width(8.dp))
with(sharedTransitionScope) {
Image(
painterResource(id = item.image),
contentDescription = item.description,
contentScale = ContentScale.Crop,
modifier = Modifier
.sharedElement(
sharedTransitionScope.rememberSharedContentState(key = "image-$index"),
animatedVisibilityScope = animatedContentScope
)
.size(100.dp)
)
Spacer(modifier = Modifier.width(8.dp))
Text(
item.name, fontSize = 18.sp,
modifier = Modifier
.align(Alignment.CenterVertically)
.sharedElement(
sharedTransitionScope.rememberSharedContentState(key = "text-$index"),
animatedVisibilityScope = animatedContentScope,
)
)
}
}
}
}
}
data class Snack(
val name: String,
val description: String,
@DrawableRes val image: Int
)
공유 요소를 사용한 뒤로 탐색 예측
공유 요소와 함께 뒤로 탐색 예측을 사용하려면 다음 단계를 따르세요.
이전 섹션의 스니펫을 사용하여 최신 navigation-compose
종속 항목을 사용합니다.
dependencies {
def nav_version = "2.8.0-beta02"
implementation "androidx.navigation:navigation-compose:$nav_version"
}
개발자 옵션에서 뒤로 탐색 예측 설정을 사용 설정합니다.
AndroidManifest.xml
파일에 android:enableOnBackInvokedCallback="true"
를 추가합니다.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:enableOnBackInvokedCallback="true"
android:theme="@style/Theme.Snippets">
그림 2. 뒤로 탐색 예측을 사용하는 탐색 Compose
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-08-30(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-08-30(UTC)"],[],[],null,["To use shared elements with the [`navigation-compose` dependency](/jetpack/compose/navigation), use the\n`Modifier.sharedElement()` that takes an `AnimatedVisibilityScope` as a\nparameter. You can use this to decide what should be visible and when.\n**Figure 1.** Navigation Compose with shared elements.\n\nThe following is an example of using `navigation-compose` with shared elements:\n\n\n```kotlin\n@Preview\n@Composable\nfun SharedElement_PredictiveBack() {\n SharedTransitionLayout {\n val navController = rememberNavController()\n NavHost(\n navController = navController,\n startDestination = \"home\"\n ) {\n composable(\"home\") {\n HomeScreen(\n this@SharedTransitionLayout,\n this@composable,\n { navController.navigate(\"details/$it\") }\n )\n }\n composable(\n \"details/{item}\",\n arguments = listOf(navArgument(\"item\") { type = NavType.IntType })\n ) { backStackEntry -\u003e\n val id = backStackEntry.arguments?.getInt(\"item\")\n val snack = listSnacks[id!!]\n DetailsScreen(\n id,\n snack,\n this@SharedTransitionLayout,\n this@composable,\n {\n navController.navigate(\"home\")\n }\n )\n }\n }\n }\n}\n\n@Composable\nfun DetailsScreen(\n id: Int,\n snack: Snack,\n sharedTransitionScope: SharedTransitionScope,\n animatedContentScope: AnimatedContentScope,\n onBackPressed: () -\u003e Unit\n) {\n with(sharedTransitionScope) {\n Column(\n Modifier\n .fillMaxSize()\n .clickable {\n onBackPressed()\n }\n ) {\n Image(\n painterResource(id = snack.image),\n contentDescription = snack.description,\n contentScale = ContentScale.Crop,\n modifier = Modifier\n .sharedElement(\n sharedTransitionScope.rememberSharedContentState(key = \"image-$id\"),\n animatedVisibilityScope = animatedContentScope\n )\n .aspectRatio(1f)\n .fillMaxWidth()\n )\n Text(\n snack.name, fontSize = 18.sp,\n modifier =\n Modifier\n .sharedElement(\n sharedTransitionScope.rememberSharedContentState(key = \"text-$id\"),\n animatedVisibilityScope = animatedContentScope\n )\n .fillMaxWidth()\n )\n }\n }\n}\n\n@Composable\nfun HomeScreen(\n sharedTransitionScope: SharedTransitionScope,\n animatedContentScope: AnimatedContentScope,\n onItemClick: (Int) -\u003e Unit,\n) {\n LazyColumn(\n modifier = Modifier\n .fillMaxSize()\n .padding(8.dp),\n verticalArrangement = Arrangement.spacedBy(8.dp)\n ) {\n itemsIndexed(listSnacks) { index, item -\u003e\n Row(\n Modifier.clickable {\n onItemClick(index)\n }\n ) {\n Spacer(modifier = Modifier.width(8.dp))\n with(sharedTransitionScope) {\n Image(\n painterResource(id = item.image),\n contentDescription = item.description,\n contentScale = ContentScale.Crop,\n modifier = Modifier\n .sharedElement(\n sharedTransitionScope.rememberSharedContentState(key = \"image-$index\"),\n animatedVisibilityScope = animatedContentScope\n )\n .size(100.dp)\n )\n Spacer(modifier = Modifier.width(8.dp))\n Text(\n item.name, fontSize = 18.sp,\n modifier = Modifier\n .align(Alignment.CenterVertically)\n .sharedElement(\n sharedTransitionScope.rememberSharedContentState(key = \"text-$index\"),\n animatedVisibilityScope = animatedContentScope,\n )\n )\n }\n }\n }\n }\n}\n\ndata class Snack(\n val name: String,\n val description: String,\n @DrawableRes val image: Int\n)https://github.com/android/snippets/blob/f95ab59fad80aeaf5d6a90bab8a01a126f20f44e/compose/snippets/src/main/java/com/example/compose/snippets/animations/sharedelement/SharedElementsWithNavigationSnippets.kt#L66-L195\n```\n\n\u003cbr /\u003e\n\nPredictive back with shared elements\n\nTo use [predictive back](/guide/navigation/custom-back/predictive-back-gesture) with shared elements, follow these steps:\n\n1. Use the latest [`navigation-compose` dependency](/develop/ui/compose/navigation), using the snippet from\n the preceding section.\n\n dependencies {\n def nav_version = \"2.8.0-beta02\"\n\n implementation \"androidx.navigation:navigation-compose:$nav_version\"\n }\n\n2. Enable the [Predictive back setting](/guide/navigation/custom-back/predictive-back-gesture#dev-option) in developer options.\n\n3. Add `android:enableOnBackInvokedCallback=\"true\"` to your `AndroidManifest.xml`\n file:\n\n \u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n \u003cuses-permission android:name=\"android.permission.INTERNET\" /\u003e\n \u003cuses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" /\u003e\n \u003cuses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\"\n android:maxSdkVersion=\"28\" /\u003e\n\n \u003capplication\n android:allowBackup=\"true\"\n android:icon=\"@mipmap/ic_launcher\"\n android:label=\"@string/app_name\"\n android:roundIcon=\"@mipmap/ic_launcher_round\"\n android:supportsRtl=\"true\"\n android:enableOnBackInvokedCallback=\"true\"\n android:theme=\"@style/Theme.Snippets\"\u003e\n\n**Figure 2.** Navigation Compose with predictive back."]]