সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
বেসিক পার্সেলযোগ্য রেসিপি
এই রেসিপিটিতে kotlinx.serialization ব্যবহার না করে, কনফিগারেশন পরিবর্তনের পরেও টিকে থাকে এমন একটি স্থায়ী ব্যাক স্ট্যাক তৈরি করার একটি প্রাথমিক উদাহরণ দেখানো হয়েছে। এর পরিবর্তে, এটি নেভিগেশন স্টেট সংরক্ষণ ও পুনরুদ্ধার করতে অ্যান্ড্রয়েডের Parcelable এবং kotlin-parcelize প্লাগইন ব্যবহার করে।
এটি কীভাবে কাজ করে
এই উদাহরণে, RouteA এবং RouteB একটি মার্কার ইন্টারফেস, Route , ইমপ্লিমেন্ট করে, যা নিজেই Parcelable এক্সটেন্ড করে। এগুলিতে kotlin-parcelize প্লাগইনের @Parcelize অ্যানোটেশনও রয়েছে, যা স্বয়ংক্রিয়ভাবে একটি Parcelable ইমপ্লিমেন্টেশন তৈরি করে:
sealed interface Route : Parcelable
@Parcelize
data object RouteA : Route
@Parcelize
data class RouteB(val id: String) : Route
ব্যাক স্ট্যাককে স্থায়ী করার জন্য, এই রেসিপিটি rememberParcelableBackStack ফাংশনটি সংজ্ঞায়িত করে। NavDisplay এবং অন্যান্য কম্পোজেবলগুলো যাতে ব্যাক স্ট্যাকের পরিবর্তন সম্পর্কে অবগত থাকে, তা নিশ্চিত করতে ব্যাক স্ট্যাকটি একটি SnapshotStateList এ সংরক্ষণ করা হয়।
@Composable
fun <T : Parcelable> rememberParcelableBackStack(vararg elements: T): SnapshotStateList<T> {
return rememberSaveable {
mutableStateListOf(*elements)
}
}
এটি androidx.navigation3.runtime এর বিল্ট-ইন rememberNavBackStack এর একটি বিকল্প হিসেবে কাজ করে, যা kotlinx.serialization উপর নির্ভরশীল। যদি আপনার অ্যাপ্লিকেশন কঠোরভাবে Parcelable পছন্দ করে এবং kotlinx.serialization উপর নির্ভর করা এড়াতে চায়, তবে এই পদ্ধতিটি ব্যবহার করুন।

অন্বেষণ করুন
সম্পূর্ণ রেসিপিটি গিটহাবে দেখুন।
arrow_forward /*
* Copyright 2026 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.basicparcelable
import android.os.Bundle
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.lifecycle.compose.dropUnlessResumed
import androidx.navigation3.runtime.NavEntry
import androidx.navigation3.ui.NavDisplay
import com.example.nav3recipes.content.ContentBlue
import com.example.nav3recipes.content.ContentGreen
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
import kotlinx.parcelize.Parcelize
sealed interface Route : Parcelable
@Parcelize
data object RouteA : Route
@Parcelize
data class RouteB(val id: String) : Route
/**
* Creates and remembers a [SnapshotStateList] to hold a back stack of [Parcelable] routes
* that survives configuration changes and process death.
*
* @param T The route type, which must implement [Parcelable].
* @param elements The initial routes to populate the back stack.
* @return A reactive [SnapshotStateList] managing the navigation back stack.
*/
@Composable
fun <T : Parcelable> rememberParcelableBackStack(vararg elements: T): SnapshotStateList<T> {
return rememberSaveable {
mutableStateListOf(*elements)
}
}
class BasicParcelableActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setEdgeToEdgeConfig()
super.onCreate(savedInstanceState)
setContent {
val backStack = rememberParcelableBackStack<Route>(RouteA)
NavDisplay(
backStack = backStack,
onBack = { backStack.removeLastOrNull() },
entryProvider = { key ->
when (key) {
is RouteA -> NavEntry(key) {
ContentGreen("Welcome to Nav3") {
Button(onClick = dropUnlessResumed {
backStack.add(RouteB("123"))
}) {
Text("Click to navigate")
}
}
}
is RouteB -> NavEntry(key) {
ContentBlue("Route id: ${key.id} ")
}
}
}
)
}
}
}
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2026-05-09 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"]],["2026-05-09 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[]]