সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
ফলাফল ফেরত দেওয়া (অবস্থা-ভিত্তিক)
এই রেসিপিটি একটি স্টেট-ভিত্তিক পদ্ধতি ব্যবহার করে এক স্ক্রিন থেকে পূর্ববর্তী স্ক্রিনে ফলাফল ফেরত পাঠানোর পদ্ধতি প্রদর্শন করে।
এটি কীভাবে কাজ করে
এই উদাহরণে ফলাফলকে স্টেট হিসেবে পরিচালনা করার জন্য একটি ResultEventBus ব্যবহার করা হয়েছে।
- ResultEventBusNavEntryDecorator : একটি
NavEntryDecorator যা LocalResultEventBus মাধ্যমে একটি ResultEventBus প্রদান করে। -
ResultEventBus : একটি ResultEventBus তৈরি করা হয় এবং LocalResultEventBus মাধ্যমে কম্পোজেবলগুলোর জন্য উপলব্ধ করা হয়। এই EventBus-টি ফলাফল প্রেরণ ও গ্রহণ করে। - ফলাফল নির্ধারণ : যে স্ক্রিনটি ফলাফল তৈরি করে, সেটি ডেটা ফেরত পাঠানোর জন্য
resultBus.sendResult(person) কল করে। - ফলাফল পর্যবেক্ষণ : যে স্ক্রিনের ফলাফল প্রয়োজন, সেটি ফলাফলকে প্রতিনিধিত্বকারী একটি
State অবজেক্ট পেতে resultBus.conflateAsState<Person?>() কল করে। এরপর UI এই স্টেটটি পর্যবেক্ষণ করে এবং যখনই ফলাফল পরিবর্তিত হয়, তখন এটিকে পুনরায় বিন্যস্ত করে।
এই পদ্ধতিটি তখন উপযুক্ত যখন শুধুমাত্র সর্বশেষ ফলাফলটির প্রয়োজন হয়। কনফিগারেশন পরিবর্তন বা প্রসেস বন্ধ হয়ে গেলে ফলাফলের অবস্থাটি অপরিবর্তিত থাকে না।

অন্বেষণ করুন
সম্পূর্ণ রেসিপিটি গিটহাবে দেখুন।
arrow_forward /*
* 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.results.common
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
class HomeViewModel : ViewModel() {
var person by mutableStateOf<Person?>(null)
}/*
* 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.results.common
import androidx.navigation3.runtime.NavKey
import kotlinx.serialization.Serializable
@Serializable
data object Home : NavKey
@Serializable
class PersonDetailsForm : NavKey
/*
* 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.results.common
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
@Parcelize
data class Person(val name: String, val favoriteColor: String) : Parcelable
/*
* 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.results.common
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.dropUnlessResumed
import com.example.nav3recipes.content.ContentBlue
import com.example.nav3recipes.content.ContentGreen
@Composable
fun HomeScreen(
person: Person?,
onNext: () -> Unit
) {
ContentBlue("Hello ${person?.name ?: "unknown person"}") {
if (person != null) {
Text("Your favorite color is ${person.favoriteColor}")
}
Spacer(Modifier.height(16.dp))
Button(onClick = dropUnlessResumed(block = onNext)) {
Text("Tell us about yourself")
}
}
}
@Composable
fun PersonDetailsScreen(
onSubmit: (Person) -> Unit
) {
ContentGreen("About you") {
val nameTextState = rememberTextFieldState()
OutlinedTextField(
state = nameTextState,
label = { Text("Please enter your name") }
)
val favoriteColorTextState = rememberTextFieldState()
OutlinedTextField(
state = favoriteColorTextState,
label = { Text("Please enter your favorite color") }
)
Button(
onClick = dropUnlessResumed {
val person = Person(
name = nameTextState.text.toString(),
favoriteColor = favoriteColorTextState.text.toString()
)
onSubmit(person)
},
enabled = nameTextState.text.isNotBlank() &&
favoriteColorTextState.text.isNotBlank()
) {
Text("Submit")
}
}
}/*
* 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.results.state
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.runtime.rememberNavBackStack
import androidx.navigation3.runtime.result.LocalResultEventBus
import androidx.navigation3.runtime.result.ResultEffect
import androidx.navigation3.runtime.result.rememberResultEventBusNavEntryDecorator
import androidx.navigation3.ui.NavDisplay
import com.example.nav3recipes.results.common.Home
import com.example.nav3recipes.results.common.HomeScreen
import com.example.nav3recipes.results.common.HomeViewModel
import com.example.nav3recipes.results.common.Person
import com.example.nav3recipes.results.common.PersonDetailsForm
import com.example.nav3recipes.results.common.PersonDetailsScreen
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
class ResultStateActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
setEdgeToEdgeConfig()
super.onCreate(savedInstanceState)
setContent {
Scaffold { paddingValues ->
val backStack = rememberNavBackStack(Home)
NavDisplay(
backStack = backStack,
modifier = Modifier.padding(paddingValues),
onBack = { backStack.removeLastOrNull() },
entryDecorators = listOf(rememberResultEventBusNavEntryDecorator()),
entryProvider = entryProvider {
entry<Home> {
val resultState = LocalResultEventBus
.current
.conflateAsState<Person?>(null)
val person = resultState.value
HomeScreen(
person = person,
onNext = { backStack.add(PersonDetailsForm()) }
)
}
entry<PersonDetailsForm> {
val resultBus = LocalResultEventBus.current
PersonDetailsScreen(
onSubmit = { person ->
resultBus.sendResult(result = person)
backStack.removeLastOrNull()
}
)
}
}
)
}
}
}
}
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2026-05-14 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-14 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[]]