Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Hướng dẫn này minh hoạ cách bạn có thể làm cho biểu tượng điều hướng trong một thanh ứng dụng trên cùng thực hiện các thao tác điều hướng.
Ví dụ
Đoạn mã sau đây là một ví dụ tối giản về cách bạn có thể triển khai một thanh ứng dụng trên cùng có biểu tượng điều hướng chức năng. Trong trường hợp này, biểu tượng sẽ đưa người dùng đến đích đến trước đó của họ trong ứng dụng:
@ComposablefunTopBarNavigationExample(navigateBack:()->Unit,){Scaffold(topBar={CenterAlignedTopAppBar(title={Text("Navigation example",)},navigationIcon={IconButton(onClick=navigateBack){Icon(imageVector=Icons.AutoMirrored.Filled.ArrowBack,contentDescription="Localized description")}},)},){innerPadding->
Text("Click the back button to pop from the back stack.",modifier=Modifier.padding(innerPadding),)}}
NavHost(navController,startDestination="home"){composable("topBarNavigationExample"){TopBarNavigationExample{navController.popBackStack()}}// Other destinations...
Tài nguyên khác
Để biết thêm thông tin về cách triển khai chế độ điều hướng trong ứng dụng, hãy xem loạt hướng dẫn sau:
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-23 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-23 UTC."],[],[],null,["This guide demonstrates how you can make the navigation icon in a [top app\nbar](https://m3.material.io/components/top-app-bar/overview) perform navigation actions.\n| **Note:** The example on this page uses `CenterAlignedTopAppBar`, but this is applicable to any app bar component with a `NavigationIcon` parameter.\n\nExample\n\nThe following snippet is a minimal example of how you can implement a top app\nbar with a functional navigation icon. In this case, the icon takes the user to\ntheir previous destination in the app:\n\n\n```kotlin\n@Composable\nfun TopBarNavigationExample(\n navigateBack: () -\u003e Unit,\n) {\n Scaffold(\n topBar = {\n CenterAlignedTopAppBar(\n title = {\n Text(\n \"Navigation example\",\n )\n },\n navigationIcon = {\n IconButton(onClick = navigateBack) {\n Icon(\n imageVector = Icons.AutoMirrored.Filled.ArrowBack,\n contentDescription = \"Localized description\"\n )\n }\n },\n )\n },\n ) { innerPadding -\u003e\n Text(\n \"Click the back button to pop from the back stack.\",\n modifier = Modifier.padding(innerPadding),\n )\n }\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/components/AppBar.kt#L353-L381\n```\n\n\u003cbr /\u003e\n\nKey points about the code\n\nNote the following in this example:\n\n- The composable `TopBarNavigationExample` defines a parameter `navigateBack` of type `() -\u003e Unit`.\n- It passes `navigateBack` for the `navigationIcon` parameter of `CenterAlignedTopAppBar`.\n\nAs such, whenever the user clicks the navigation icon in the top app back, it\ncalls `navigateBack()`.\n\nPass a function\n\nThis example uses a back arrow for the icon. As such, the argument for\n`navigateBack()` should take the user to the previous destination.\n\nTo do so, pass `TopBarNavigationExample` a call to\n[`NavController.popBackStack()`](/guide/navigation/backstack). You do this where you [build your\nnavigation](/guide/navigation/design#compose) graph. For example: \n\n NavHost(navController, startDestination = \"home\") {\n composable(\"topBarNavigationExample\") {\n TopBarNavigationExample{ navController.popBackStack() }\n }\n // Other destinations...\n\n| **Note:** Using the same approach, you could implement a different action for the navigation icon. For example, you could use a house icon and pass a call to `navController.navigate(\"home\")`.\n\nAdditional resources\n\nFor more information on how to implement navigation in your app, see the\nfollowing series of guides:\n\n- [Navigation with Compose](/develop/ui/compose/navigation)\n- [Create a NavController](/develop/ui/compose/navigation#navcontroller)\n- [Design your navigation graph](/guide/navigation/design#compose)\n- [Navigate to a composable](/develop/ui/compose/navigation#nav-to-composable)"]]