تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يمكنك استخدام واجهات برمجة التطبيقات المضمّنة التي تتضمّن أمانًا من النوع لتوفير أمان من النوع في وقت الترجمة ل graph
التنقّل. تتوفّر واجهات برمجة التطبيقات هذه عندما يستخدم تطبيقك Navigation
Compose أو Navigation Kotlin DSL. وأصبحت هذه الميزة متاحة اعتبارًا من Navigation
2.8.0.
هذه واجهات برمجة التطبيقات مكافئة لما تقدّمه Safe Args لرسومات التنقّل
المُنشأة باستخدام XML.
تحديد المسارات
لاستخدام المسارات الآمنة من حيث النوع في أداة Compose، عليك أولاً تحديد klassen أو عناصر قابلة للتسلسل تمثّل مساراتك.
استخدِم القواعد التالية لتحديد النوع الذي تريد استخدامه لمسارك:
كائن: استخدِم كائنًا للمسارات بدون وسيطات.
الفئة: استخدِم فئة أو فئة بيانات للمسارات التي تحتوي على وسيطات.
KClass<T>: استخدِم هذا الأسلوب إذا لم تكن بحاجة إلى تمرير الوسيطات، مثل فئة
بدون مَعلمات أو فئة تتضمّن جميع المَعلمات قيمًا تلقائية
على سبيل المثال: Profile::class
في جميع الحالات، يجب أن يكون الكائن أو الفئة قابلَين للتسلسل.
مثلاً:
// Define a home route that doesn't take any arguments@SerializableobjectHome// Define a profile route that takes an ID@SerializabledataclassProfile(valid:String)
إنشاء الرسم البياني
بعد ذلك، عليك تحديد الرسم البياني للتنقّل. استخدِم الدالة composable()
لتحديد العناصر القابلة للتجميع كوجهات في الرسم البياني للتنقّل.
تأخذ composable() مَعلمة نوع. أي composable<Profile>.
يُعدّ تحديد نوع الوجهة نهجًا أكثر فاعلية من تمرير سلسلةroute كما هو الحال في composable("profile").
تحدّد فئة المسار نوع كلّ مَعلمة تنقّل، كما هو الحال في val id:
String، لذا ليس هناك حاجة إلى NavArgument.
بالنسبة إلى مسار الملف الشخصي، تعيد طريقة إضافة toRoute() إنشاء عنصر
Profile من NavBackStackEntry ووسيطاته.
لمزيد من المعلومات حول كيفية تصميم الرسم البياني بشكل عام، يُرجى الاطّلاع على صفحة تصميم
الرسم البياني للتنقّل.
الانتقال إلى مسار آمن للكتابة
أخيرًا، يمكنك الانتقال إلى العنصر القابل للتجميع باستخدام الدالة navigate()
من خلال إدخال مثيل المسار:
navController.navigate(Profile(id=123))
يؤدي ذلك إلى توجيه المستخدِم إلى الوجهة composable<Profile> في
رسم بياني التنقّل. يمكن الحصول على أي وسيطات تنقّل، مثل id، من خلال
إعادة إنشاء Profile باستخدام NavBackStackEntry.toRoute وقراءة
خصائصه.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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"]],["تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Type safety in Kotlin DSL and Navigation Compose\n\nYou can use built-in type safe APIs to provide compile-time type safety for your\nnavigation graph. These APIs are available when your app uses the [Navigation\nCompose](/jetpack/compose/navigation) or [Navigation Kotlin DSL](/guide/navigation/navigation-kotlin-dsl). They are available as of `Navigation\n2.8.0`.\n\nThese APIs are equivalent to what [Safe Args](/guide/navigation/navigation-pass-data#Safe-args) provides to navigation graphs\nbuilt using XML.\n\nDefine routes\n-------------\n\nTo use type-safe routes in Compose, you first need to define serializable\nclasses or objects that represent your routes.\n\nTo define serializable objects use `@Serializable` annotation provided by the\n[Kotlin Serialization plugin](https://kotlinlang.org/docs/serialization.html).\nThis plugin can be added to your project by [adding these\ndependencies](/guide/navigation#set-up).\n\nUse the following rules to decide what type to use for your route:\n\n- **Object**: Use an object for routes without arguments.\n- **Class**: Use a class or data class for routes with arguments.\n- **`KClass\u003cT\u003e`** : Use if you don't need to pass arguments, such as a class without parameters, or a class where all parameters have default values\n 1. For example: `Profile::class`\n\nIn all cases the object or class must be serializable.\n\nFor example: \n\n // Define a home route that doesn't take any arguments\n @Serializable\n object Home\n\n // Define a profile route that takes an ID\n @Serializable\n data class Profile(val id: String)\n\n### Build your graph\n\nNext, you need to define your navigation graph. Use the [`composable()`](/reference/kotlin/androidx/navigation/NavGraphBuilder#(androidx.navigation.NavGraphBuilder).composable(kotlin.collections.Map,kotlin.collections.List,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function2))\nfunction to define composables as destinations in your navigation graph. \n\n NavHost(navController, startDestination = Home) {\n composable\u003cHome\u003e {\n HomeScreen(onNavigateToProfile = { id -\u003e\n navController.navigate(Profile(id))\n })\n }\n composable\u003cProfile\u003e { backStackEntry -\u003e\n val profile: Profile = backStackEntry.toRoute()\n ProfileScreen(profile.id)\n }\n }\n\nObserve the following in this example:\n\n- `composable()` takes a type parameter. That is, `composable\u003cProfile\u003e`.\n- Defining the destination type is a more robust approach than passing a [`route`](/reference/kotlin/androidx/navigation/NavGraphBuilder#(androidx.navigation.NavGraphBuilder).composable(kotlin.String,kotlin.collections.List,kotlin.collections.List,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function2)) [string](/reference/kotlin/androidx/navigation/NavGraphBuilder#(androidx.navigation.NavGraphBuilder).composable(kotlin.String,kotlin.collections.List,kotlin.collections.List,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function2)) as in `composable(\"profile\")`.\n- The route class defines the type of each navigation argument, as in `val id:\n String`, so there's no need for [`NavArgument`](/reference/kotlin/androidx/navigation/NavArgument).\n- For the profile route, the `toRoute()` extension method recreates the `Profile` object from the [`NavBackStackEntry`](/reference/androidx/navigation/NavBackStackEntry) and its arguments.\n\nFor more information on how to design your graph in general, see the [Design\nyour Navigation graph](/guide/navigation/design) page.\n\n### Navigate to type safe route\n\nFinally, you can navigate to your composable using the [`navigate()`](/reference/kotlin/androidx/navigation/NavGraphBuilder#(androidx.navigation.NavGraphBuilder).composable(kotlin.collections.Map,kotlin.collections.List,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function2))\nfunction by passing in the instance of the route: \n\n navController.navigate(Profile(id = 123))\n\nThis navigates the user to the `composable\u003cProfile\u003e` destination in the\nnavigation graph. Any navigation arguments, such as `id`, can be obtained by\nreconstructing `Profile` using `NavBackStackEntry.toRoute` and reading its\nproperties.\n| **Important:** Because the parameters of the data class are typed, when you pass an instance of that class to `navigate()`, the arguments are necessarily type safe.\n\n### Additional resources\n\n- [Design your navigation graph](/guide/navigation/design)\n- [Use your navigation graph](/guide/navigation/use-graph)"]]