أفضل الممارسات المتعلّقة بالتفاعل مع خريطة التنقّل
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
توضّح هذه الصفحة أفضل الممارسات للتفاعل مع "خريطة التنقّل" في تطبيقك.
استخدِم SupportNavigationFragment
بدلاً من NavigationView
، متى أمكن ذلك
SupportNavigationFragment
هو برنامج تضمين لتسهيل عملية التعامل مع عمليات معاودة الاتصال لدورة حياة NavigationView
، وبالتالي لن تحتاج إلى إدارة عمليات معاودة الاتصال هذه بنفسك. هذه الطريقة أقل عرضة للأخطاء وهي الطريقة المفضّلة لاستخدام Navigation في تطبيقك. عند استخدام SupportNavigationFragment
، احرص على عدم استدعاء أحداث دورة حياة NavigationView
.
في حال استخدام NavigationView
، استخدِم ترتيبًا صارمًا عند استدعاء طرق دورة الحياة
يستضيف NavigationView
خريطة التنقّل ويتتبّع عن كثب أحداث مراحل النشاط مثل أنشطة Android وأجزائه، ويتّخذ إجراءات محدّدة عند استدعاء أحداث مراحل النشاط هذه. تنفّذ NavigationView
عمليات تهيئة متعددة على NavigationView#onCreate
وNavigationView#onStart
، وعمليات تنظيف على NavigationView#onStop
وNavigationView#onDestroy
، وكذلك عند معالجة أحداث أخرى لدورة الحياة.
تتضمّن طرق دورة حياة NavigationView
المعنى نفسه الذي تتضمّنه في أنشطة Android أو أقسامه. على سبيل المثال، يمكن ترجمة onCreate()
من NavigationView
تقريبًا إلى عمليات رد الاتصال بدورة الحياة التي يتم استدعاؤها من نشاط Android أو جزء منه. بما أنّ عمليات معاودة الاتصال بدورة حياة NavigationView
تستند إلى عمليات معاودة الاتصال بدورة حياة Android ويتم استدعاؤها بالترتيب نفسه، يجب ترتيب طرق NavigationView هذه بشكل صارم. وفي حال عدم توفّرها، قد تواجه تسرّبًا للذاكرة وأخطاء في واجهة المستخدم وعدم تعديل الموقع الجغرافي ومشاكل أخرى.
لمزيد من المعلومات عن دورة حياة الأنشطة على Android، راجِع قسم
مفاهيم دورة حياة الأنشطة
في مستندات مطوّري تطبيقات Android.
يوضّح الجدول التالي الحالات التي يجب فيها استدعاء طرق مراحل النشاط الأخرى بعد طرق مراحل النشاط المحدّدة:
طريقة مراحل النشاط |
يتم استدعاؤها في أي مرحلة من مراحل نشاط التطبيق |
يتم استدعاؤها بعد طريقة دورة الحياة التالية |
onConfigurationChanged()
|
يتم استدعاؤه عندما تكون واجهة المستخدم في المقدّمة وتتغير الإعدادات.
|
دائمًا بعد الساعة onStart()
|
onTrimMemory()
|
يتم استدعاؤه عندما يكون أحد الأنشطة في الخلفية.
|
دائمًا بعد الساعة onPause()
|
onSaveInstance()
|
يتم استدعاؤها قبل إيقاف نشاط.
|
دائمًا بعد onStop()
|
لا تستدعِ طرق دورة الحياة هذه عدة مرات بدون استدعاء طريقة الإغلاق المقابلة أولاً. بالإضافة إلى ذلك، يُرجى العِلم أنّه إذا كان التطبيق نفسه يدير بعض عمليات معاودة الاتصال بدورة حياة Android هذه، وتمت إضافة NavigationView
إلى الجزء بعد الإنشاء أو البدء، يجب أن يستدعي التطبيق الطرق المحدّدة نفسها بالترتيب الصحيح من أجل إعداد حزمة تطوير البرامج (SDK) الخاصة بخدمة Navigation بشكل صحيح.
للحصول على إرشادات إضافية حول استخدام هذه الطرق، راجِع تطبيق العرض التوضيحي لحزمة Navigation SDK.
في حال استخدام NavigationView
، استدعِ أحداث مراحل النشاط من النشاط أو الجزء، وليس كليهما.
للحفاظ على الترتيب الدقيق لطُرق مراحل النشاط، استدعِ هذه الأحداث من عمليات معاودة الاتصال الخاصة بمراحل نشاط النشاط أو الجزء، والتي تتلقّى هذه الأحداث بالترتيب. تضمن هذه الطريقة عدم حاجة التطبيقات إلى التنسيق بين الأجزاء والأنشطة، ما يؤدي إلى تجنُّب تكرار عمليات الاستدعاء.
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 2.0. للاطّلاع على التفاصيل، يُرجى مراجعة سياسات موقع Google Developers. إنّ Java هي علامة تجارية مسجَّلة لشركة Oracle و/أو شركائها التابعين.
تاريخ التعديل الأخير: 2025-08-31 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-31 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003ePrioritize using \u003ccode\u003eSupportNavigationFragment\u003c/code\u003e over \u003ccode\u003eNavigationView\u003c/code\u003e for simplified lifecycle management and error reduction.\u003c/p\u003e\n"],["\u003cp\u003eWhen using \u003ccode\u003eNavigationView\u003c/code\u003e directly, ensure strict adherence to the Android lifecycle order when invoking its lifecycle methods to prevent issues like memory leaks and UI errors.\u003c/p\u003e\n"],["\u003cp\u003eInvoke \u003ccode\u003eNavigationView\u003c/code\u003e lifecycle events exclusively from either the activity or the fragment to maintain order and avoid duplicate calls.\u003c/p\u003e\n"]]],[],null,["# Navigation map interaction best practices\n\nThis page explains best practices for interacting with the Navigation map in\nyour app.\n\nUse `SupportNavigationFragment` instead of `NavigationView`, whenever possible\n------------------------------------------------------------------------------\n\n`SupportNavigationFragment` is a wrapper for the convenience of handling the\n`NavigationView` lifecycle callbacks, so you don't need to manage these\ncallbacks themselves. This method is less error-prone and the preferred way to\nuse Navigation in your app. When using `SupportNavigationFragment`, be sure not\nto invoke `NavigationView` lifecycle events.\n\nIf using `NavigationView`, use strict ordering when invoking lifecycle methods\n------------------------------------------------------------------------------\n\n`NavigationView` hosts the Navigation map and closely follows the lifecycle\nevents as Android activities and fragments, taking specific actions when these\nlifecycle events are invoked. `NavigationView` executes multiple initializations\non `NavigationView#onCreate` and `NavigationView#onStart`, and cleanups on\n`NavigationView#onStop` and `NavigationView#onDestroy`, as well as when other\nlifecycle events are processed.\n\n`NavigationView` lifecycle methods have the same meaning as they do for Android\nactivities or fragments. For example, `onCreate()` of `NavigationView` roughly\ntranslates to and should be invoked by lifecycle callbacks from the Android\nactivity or fragment. Since the `NavigationView` lifecycle callbacks are based\non and invoked in the same order as the Android lifecycle callbacks, strong\nordering of these NavigationView methods is required. Otherwise, you may\nexperience [memory\nleaks](/maps/documentation/navigation/android-sdk/memory-best-practices), UI\nerrors, location not being updated, and other issues.\n\nFor more information about the Android activity lifecycle, see the\n[Activity-lifecycle concepts](https://developer.android.com/guide/components/activities/activity-lifecycle#alc)\nsection in the Android developer documentation.\n\nThe following table shows when other lifecycle methods should be invoked, after\nspecified lifecycle methods:\n\n| Lifecycle method | Invoked where in the activity lifecycle | Invoked after which lifecycle method |\n|----------------------------|-------------------------------------------------------------------------|--------------------------------------|\n| `onConfigurationChanged()` | Invoked when the UI is in the foreground and the configuration changes. | Always after `onStart()` |\n| `onTrimMemory()` | Invoked when an activity is in the background. | Always after `onPause()` |\n| `onSaveInstance()` | Invoked before an activity is destroyed. | Always after` onStop()` |\n\nDon't call these lifecycle methods multiple times without calling the\ncorresponding closing method first. In addition, keep in mind that if some of\nthese Android lifecycle callbacks are managed by the app itself, and the\n`NavigationView` is added to the fragment after creation or start, the app\nshould call the specific methods themselves in proper order in order to\ncorrectly initialize the Navigation SDK.\n\nFor additional guidance on using these methods, see the [Navigation SDK demo\napp](https://github.com/googlemaps-samples/android-navigation-samples/blob/4afba69715ebf6c176a2eb438f86d06c23065e7f/navigation-sample/app/src/main/java/com/example/navigationapidemo/NavViewActivity.kt#L279).\n\nIf using `NavigationView`, invoke lifecycle events from the activity or fragment, not both\n------------------------------------------------------------------------------------------\n\nTo keep the strict ordering of the lifecycle methods, invoke these events from\neither the activity or fragment lifecycle callbacks, which receive these events\nin order. This approach ensures that apps don't need to coordinate between\nfragments and activities and cause duplicate calls."]]