با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
دستور العمل ناوبری ماژولار (کوین)
این دستورالعمل نحوه ساختاردهی یک برنامه چند ماژوله با استفاده از Navigation 3 و Koin برای تزریق وابستگی را نشان میدهد. هدف، ایجاد یک معماری جدا شده است که در آن navigation در ماژولهای ویژگی جداگانه تعریف و پیادهسازی میشود. این معماری به مصنوع koin-compose-navigation3 متکی است.
چگونه کار میکند؟
این برنامه به چندین ماژول اندروید تقسیم شده است:
ماژول app : این ماژول اصلی برنامه است. این ماژولهای ویژگی را includes() و یک Navigator مشترک را مقداردهی اولیه میکند.
ماژول common : این ماژول شامل منطق ناوبری اصلی است که توسط ماژول برنامه و ماژولهای ویژگی استفاده میشود. به عبارت دیگر، یک کلاس Navigator تعریف میکند که back stack را مدیریت میکند.
ماژولهای ویژگی (مثلاً conversation ، profile ) : هر ویژگی به دو زیرماژول تقسیم میشود:
- ماژول
api : API عمومی برای این ویژگی، شامل مسیرهای ناوبری آن را تعریف میکند. این به ماژولهای دیگر اجازه میدهد بدون نیاز به دانستن جزئیات پیادهسازی آن، به این ویژگی دسترسی پیدا کنند. - ماژول
impl : پیادهسازی این ویژگی، شامل کامپوننتهای آن و Module Koin را فراهم میکند. ماژول Koin از DSL navigation برای تعریف نصبکنندههای ارائهدهنده ورودی برای ماژول ویژگی استفاده میکند.
این رویکرد ماژولار امکان جداسازی دقیق دغدغهها را فراهم میکند و کدبیس را مقیاسپذیرتر و قابل نگهداریتر میسازد. هر ویژگی مسئول منطق ناوبری خود است و ماژول app فقط این قطعات را با هم ترکیب میکند.

کاوش
دستور پخت کامل را در گیتهاب ببینید.
arrow_forward package com.example.nav3recipes.modular.koin
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import org.koin.androidx.scope.dsl.activityRetainedScope
import org.koin.core.annotation.KoinExperimentalAPI
import org.koin.dsl.module
import org.koin.dsl.navigation3.navigation
// API
object Profile
// IMPL
@OptIn(KoinExperimentalAPI::class)
val profileModule = module {
activityRetainedScope {
navigation<Profile> { ProfileScreen() }
}
}
@Composable
private fun ProfileScreen() {
val profileColor = MaterialTheme.colorScheme.surfaceVariant
Column(
modifier = Modifier
.fillMaxSize()
.background(profileColor)
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = "Profile Screen",
style = MaterialTheme.typography.headlineMedium,
color = MaterialTheme.colorScheme.onSurface
)
}
}
package com.example.nav3recipes.modular.koin
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Button
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.dropUnlessResumed
import com.example.nav3recipes.ui.theme.colors
import org.koin.androidx.scope.dsl.activityRetainedScope
import org.koin.core.annotation.KoinExperimentalAPI
import org.koin.dsl.module
import org.koin.dsl.navigation3.navigation
// API
object ConversationList
data class ConversationDetail(val id: Int) {
val color: Color
get() = colors[id % colors.size]
}
// IMPL
@OptIn(KoinExperimentalAPI::class)
val conversationModule = module {
activityRetainedScope {
navigation<ConversationList> {
ConversationListScreen(
onConversationClicked = { conversationDetail ->
get<Navigator>().goTo(conversationDetail)
}
)
}
navigation<ConversationDetail> { key ->
ConversationDetailScreen(key) {
get<Navigator>().goTo(Profile)
}
}
}
}
@Composable
private fun ConversationListScreen(
onConversationClicked: (ConversationDetail) -> Unit
) {
LazyColumn(
modifier = Modifier.fillMaxSize(),
) {
items(10) { index ->
val conversationId = index + 1
val conversationDetail = ConversationDetail(conversationId)
val backgroundColor = conversationDetail.color
ListItem(
modifier = Modifier
.fillMaxWidth()
.clickable(onClick = dropUnlessResumed {
onConversationClicked(conversationDetail)
}),
headlineContent = {
Text(
text = "Conversation $conversationId",
style = MaterialTheme.typography.headlineSmall,
color = MaterialTheme.colorScheme.onSurface
)
},
colors = ListItemDefaults.colors(
containerColor = backgroundColor // Set container color directly
)
)
}
}
}
@Composable
private fun ConversationDetailScreen(
conversationDetail: ConversationDetail,
onProfileClicked: () -> Unit
) {
Column(
modifier = Modifier
.fillMaxSize()
.background(conversationDetail.color)
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = "Conversation Detail Screen: ${conversationDetail.id}",
style = MaterialTheme.typography.headlineMedium,
color = MaterialTheme.colorScheme.onSurface
)
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = dropUnlessResumed(block = onProfileClicked)) {
Text("View Profile")
}
}
}
package com.example.nav3recipes.modular.koin
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.snapshots.SnapshotStateList
class Navigator(startDestination: Any) {
val backStack : SnapshotStateList<Any> = mutableStateListOf(startDestination)
fun goTo(destination: Any){
backStack.add(destination)
}
fun goBack(){
backStack.removeLastOrNull()
}
}package com.example.nav3recipes.modular.koin
import org.koin.androidx.scope.dsl.activityRetainedScope
import org.koin.dsl.module
val appModule = module {
includes(profileModule,conversationModule)
activityRetainedScope {
scoped {
Navigator(startDestination = ConversationList)
}
}
}package com.example.nav3recipes.modular.koin
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.navigation3.ui.NavDisplay
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
import org.koin.android.ext.android.inject
import org.koin.android.scope.AndroidScopeComponent
import org.koin.androidx.compose.navigation3.getEntryProvider
import org.koin.androidx.scope.activityRetainedScope
import org.koin.core.Koin
import org.koin.core.annotation.KoinExperimentalAPI
import org.koin.core.component.KoinComponent
import org.koin.core.scope.Scope
import org.koin.dsl.koinApplication
/**
* This recipe demonstrates how to use a modular approach with Navigation 3,
* where different parts of the application are defined in separate modules and injected
* into the main app using Koin.
*
* Features (Conversation and Profile) are split into two modules:
* - api: defines the public facing routes for this feature
* - impl: defines the entryProviders for this feature, these are injected into the app's main activity
* The common module defines:
* - a common navigator class that exposes a back stack and methods to modify that back stack
* - a type that should be used by feature modules to inject entryProviders into the app's main activity
* The app module creates the navigator by supplying a start destination and provides this navigator
* to the rest of the app module (i.e. MainActivity) and the feature modules.
*/
@OptIn(KoinExperimentalAPI::class)
class KoinModularActivity : ComponentActivity(), AndroidScopeComponent, KoinComponent {
// Local Koin Context Instance
companion object {
private val localKoin = koinApplication {
modules(appModule)
}.koin
}
// Override default Koin context to use the local one
override fun getKoin(): Koin = localKoin
override val scope : Scope by activityRetainedScope()
val navigator: Navigator by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setEdgeToEdgeConfig()
setContent {
Scaffold { paddingValues ->
NavDisplay(
backStack = navigator.backStack,
modifier = Modifier.padding(paddingValues),
onBack = { navigator.goBack() },
entryProvider = getEntryProvider()
)
}
}
}
}
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2026-05-09 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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 بهوقت ساعت هماهنگ جهانی."],[],[]]