यूज़र इंटरफ़ेस (यूआई) की सामान्य रेसिपी
इस रेसिपी में, नेविगेशन यूज़र इंटरफ़ेस (यूआई) के एक सामान्य पैटर्न को लागू करने का तरीका बताया गया है. इसमें सबसे नीचे मौजूद नेविगेशन बार और कई बैक स्टैक शामिल होते हैं. नेविगेशन बार में मौजूद हर टैब की अपनी नेविगेशन हिस्ट्री होती है.
यह कैसे काम करता है
इस उदाहरण में तीन टॉप-लेवल डेस्टिनेशन हैं: Home, ChatList, और Camera. ChatList डेस्टिनेशन का एक सब-रूट भी है, ChatDetail.
TopLevelBackStack
इस रेसिपी का मुख्य हिस्सा TopLevelBackStack क्लास है. यह नेविगेशन की स्थिति को मैनेज करने के लिए ज़िम्मेदार है. यह इस तरह काम करता है:
- यह हर टॉप-लेवल डेस्टिनेशन (टैब) के लिए, अलग बैक स्टैक बनाए रखता है.
- यह कुकी, मौजूदा समय में चुने गए टॉप-लेवल डेस्टिनेशन को ट्रैक करती है.
- यह एक सिंगल, फ़्लैट बैक स्टैक उपलब्ध कराता है. इसका इस्तेमाल
NavDisplayकंपोज़ेबल कर सकता है. यह फ़्लैट किया गया बैक स्टैक, सभी टैब के अलग-अलग बैक स्टैक का कॉम्बिनेशन होता है.
यूज़र इंटरफ़ेस (यूआई) का स्ट्रक्चर
यूज़र इंटरफ़ेस (यूआई) को Scaffold कंपोज़ेबल का इस्तेमाल करके बनाया गया है. इसमें NavigationBar को bottomBar के तौर पर इस्तेमाल किया गया है.
NavigationBarमें, हर टॉप-लेवल डेस्टिनेशन के लिए एक आइटम दिखाया जाता है. किसी आइटम पर क्लिक करने पर, यहtopLevelBackStack.addTopLevelको कॉल करता है. इससे, उपयोगकर्ता को उससे जुड़े टैब पर ले जाया जाता है. साथ ही, हर टैब के नेविगेशन इतिहास को सेव रखा जाता है.NavDisplayकंपोज़ेबल कोScaffoldके कॉन्टेंट एरिया में रखा गया है. यहTopLevelBackStackसे मिले फ़्लैट किए गए बैक स्टैक के आधार पर, मौजूदा स्क्रीन को दिखाने के लिए ज़िम्मेदार होता है.
इस अप्रोच से, नेविगेशन का एक सामान्य पैटर्न मिलता है. इसमें उपयोगकर्ता, ऐप्लिकेशन के अलग-अलग सेक्शन के बीच स्विच कर सकते हैं. साथ ही, हर सेक्शन अपनी नेविगेशन हिस्ट्री बनाए रखता है.
स्टेट को बनाए रखना
यह जानना ज़रूरी है कि इस रेसिपी में नेविगेशन की स्थिति को कैसे मैनेज किया जाता है. जब कोई उपयोगकर्ता टॉप-लेवल डेस्टिनेशन से हट जाता है (उदाहरण के लिए, 'वापस जाएं' बटन को तब तक दबाता है, जब तक वह पिछले टैब पर वापस नहीं आ जाता), तो उस डेस्टिनेशन के लिए नेविगेशन का पूरा इतिहास मिट जाता है. राज्य का नाम सेव नहीं किया गया है. जब उपयोगकर्ता बाद में उस टैब पर वापस आएगा, तो उसे शुरुआती स्क्रीन से शुरू करना होगा.
ध्यान दें: इस उदाहरण में, Home रूट को ChatList और Camera रूट के ऊपर ले जाया जा सकता है. इसका मतलब है कि Home से वापस नेविगेट करने पर, ऐप्लिकेशन बंद नहीं होगा. ऐप्लिकेशन तब बंद होगा, जब उपयोगकर्ता बैक स्टैक में मौजूद एक ही टॉप लेवल रूट से वापस जाएगा.
/* * 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() } }