สูตร UI ทั่วไป
สูตรนี้แสดงวิธีใช้รูปแบบ UI การนำทางทั่วไปที่มีแถบนำทางด้านล่างและ Back Stack หลายรายการ โดยที่แต่ละแท็บในแถบนำทางจะมีประวัติการนำทางของตัวเอง
วิธีการทำงาน
ตัวอย่างนี้มีปลายทางระดับบนสุด 3 รายการ ได้แก่ Home, ChatList และ Camera ChatList ปลายทางยังมีเส้นทางย่อย ChatDetail ด้วย
TopLevelBackStack
หัวใจหลักของสูตรนี้คือคลาส TopLevelBackStack ซึ่งมีหน้าที่จัดการสถานะการนำทาง ซึ่งจะทำงานดังนี้
- โดยจะรักษาสแต็กย้อนกลับแยกต่างหากสำหรับปลายทางระดับบนสุด (แท็บ) แต่ละรายการ
- โดยจะติดตามปลายทางระดับบนสุดที่เลือกในปัจจุบัน
- โดยจะแสดงผลสแต็กย้อนกลับแบบแบนรายการเดียวที่
NavDisplayComposable ใช้ได้ สแต็กย้อนกลับที่ยุบนี้เป็นการรวมสแต็กย้อนกลับแต่ละรายการของแท็บทั้งหมด
โครงสร้าง UI
UI สร้างขึ้นโดยใช้ Scaffold ที่ใช้ร่วมกันได้ โดยมี NavigationBar เป็น bottomBar
NavigationBarจะแสดงรายการสำหรับปลายทางระดับบนสุดแต่ละรายการ เมื่อคลิกรายการ ระบบจะเรียกใช้topLevelBackStack.addTopLevelเพื่อเปลี่ยนไปใช้แท็บที่เกี่ยวข้อง โดยจะเก็บประวัติการนำทางของแต่ละแท็บไว้- ระบบจะวาง
NavDisplayที่ใช้ร่วมกันได้ในพื้นที่เนื้อหาของScaffoldโดยมีหน้าที่แสดงหน้าจอปัจจุบันตาม Back Stack ที่TopLevelBackStackจัดเตรียมให้
แนวทางนี้ช่วยให้มีรูปแบบการนำทางทั่วไปที่ผู้ใช้สามารถสลับระหว่างส่วนต่างๆ ของแอปได้ และแต่ละส่วนจะยังคงมีประวัติการนำทางของตัวเอง
การเก็บรักษาสถานะ
คุณควรทราบวิธีจัดการสถานะการนำทางในสูตรนี้ เมื่อผู้ใช้ออกจากปลายทางระดับบนสุด (เช่น โดยการกดปุ่มย้อนกลับจนกว่าจะกลับไปที่แท็บก่อนหน้า) ระบบจะล้างประวัติการนำทางทั้งหมดสำหรับปลายทางนั้น ไม่ได้บันทึกรัฐ เมื่อผู้ใช้กลับไปที่แท็บนั้นในภายหลัง ผู้ใช้จะเริ่มจากหน้าจอเริ่มต้นของแท็บ
หมายเหตุ: ในตัวอย่างนี้ เส้นทาง Home สามารถเลื่อนขึ้นเหนือเส้นทาง ChatList และ Camera ได้ ซึ่งหมายความว่าการกลับจาก Home ไม่จำเป็นต้องออกจากแอป แอปจะปิดเมื่อผู้ใช้กลับจากเส้นทางระดับบนสุดที่เหลืออยู่เพียงเส้นทางเดียวใน Back Stack
/* * Copyright 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.nav3recipes.commonui import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Face import androidx.compose.material.icons.filled.Home import androidx.compose.material.icons.filled.PlayArrow import androidx.compose.material3.Button import androidx.compose.material3.Icon import androidx.compose.material3.NavigationBar import androidx.compose.material3.NavigationBarItem import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.runtime.snapshots.SnapshotStateList import androidx.compose.ui.graphics.vector.ImageVector import androidx.lifecycle.compose.dropUnlessResumed import androidx.navigation3.runtime.entryProvider import androidx.navigation3.ui.NavDisplay import com.example.nav3recipes.content.ContentBlue import com.example.nav3recipes.content.ContentGreen import com.example.nav3recipes.content.ContentPurple import com.example.nav3recipes.content.ContentRed import com.example.nav3recipes.ui.setEdgeToEdgeConfig private sealed interface TopLevelRoute { val icon: ImageVector } private data object Home : TopLevelRoute { override val icon = Icons.Default.Home } private data object ChatList : TopLevelRoute { override val icon = Icons.Default.Face } private data object ChatDetail private data object Camera : TopLevelRoute { override val icon = Icons.Default.PlayArrow } private val TOP_LEVEL_ROUTES : List<TopLevelRoute> = listOf(Home, ChatList, Camera) class CommonUiActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { setEdgeToEdgeConfig() super.onCreate(savedInstanceState) setContent { val topLevelBackStack = remember { TopLevelBackStack<Any>(Home) } Scaffold( bottomBar = { NavigationBar { TOP_LEVEL_ROUTES.forEach { topLevelRoute -> val isSelected = topLevelRoute == topLevelBackStack.topLevelKey NavigationBarItem( selected = isSelected, onClick = { topLevelBackStack.addTopLevel(topLevelRoute) }, icon = { Icon( imageVector = topLevelRoute.icon, contentDescription = null ) } ) } } } ) { _ -> NavDisplay( backStack = topLevelBackStack.backStack, onBack = { topLevelBackStack.removeLast() }, entryProvider = entryProvider { entry<Home>{ ContentRed("Home screen") } entry<ChatList>{ ContentGreen("Chat list screen"){ Button(onClick = dropUnlessResumed { topLevelBackStack.add(ChatDetail) }) { Text("Go to conversation") } } } entry<ChatDetail>{ ContentBlue("Chat detail screen") } entry<Camera>{ ContentPurple("Camera screen") } }, ) } } } } class TopLevelBackStack<T: Any>(startKey: T) { // Maintain a stack for each top level route private var topLevelStacks : LinkedHashMap<T, SnapshotStateList<T>> = linkedMapOf( startKey to mutableStateListOf(startKey) ) // Expose the current top level route for consumers var topLevelKey by mutableStateOf(startKey) private set // Expose the back stack so it can be rendered by the NavDisplay val backStack = mutableStateListOf(startKey) private fun updateBackStack() = backStack.apply { clear() addAll(topLevelStacks.flatMap { it.value }) } fun addTopLevel(key: T){ // If the top level doesn't exist, add it if (topLevelStacks[key] == null){ topLevelStacks.put(key, mutableStateListOf(key)) } else { // Otherwise just move it to the end of the stacks topLevelStacks.apply { remove(key)?.let { put(key, it) } } } topLevelKey = key updateBackStack() } fun add(key: T){ topLevelStacks[topLevelKey]?.add(key) updateBackStack() } fun removeLast(){ val removedKey = topLevelStacks[topLevelKey]?.removeLastOrNull() // If the removed key was a top level key, remove the associated top level stack topLevelStacks.remove(removedKey) topLevelKey = topLevelStacks.keys.last() updateBackStack() } }