แนวทางปฏิบัติแนะนำสำหรับการโต้ตอบกับแผนที่การนำทาง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หน้านี้อธิบายแนวทางปฏิบัติแนะนำสำหรับการโต้ตอบกับแผนที่การนำทางในแอป
ใช้ SupportNavigationFragment
แทน NavigationView
ทุกครั้งที่เป็นไปได้
SupportNavigationFragment
เป็น Wrapper เพื่อความสะดวกในการจัดการ
NavigationView
การเรียกกลับของวงจรการใช้งาน คุณจึงไม่จำเป็นต้องจัดการการเรียกกลับเหล่านี้ด้วยตนเอง วิธีนี้มีข้อผิดพลาดน้อยกว่าและเป็นวิธีที่แนะนำในการ
ใช้การนำทางในแอป เมื่อใช้ SupportNavigationFragment
โปรดอย่า
เรียกใช้เหตุการณ์วงจรของ NavigationView
หากใช้ NavigationView
ให้ใช้การจัดลำดับที่เข้มงวดเมื่อเรียกใช้เมธอดวงจร
NavigationView
จะโฮสต์แผนที่การนำทางและติดตามเหตุการณ์วงจร
อย่างใกล้ชิดในฐานะกิจกรรมและ Fragment ของ Android โดยจะดำเนินการเฉพาะเมื่อมีการเรียกใช้เหตุการณ์วงจร
เหล่านี้ NavigationView
จะดำเนินการเริ่มต้นหลายครั้ง
ใน NavigationView#onCreate
และ NavigationView#onStart
รวมถึงการล้างข้อมูลใน
NavigationView#onStop
และ NavigationView#onDestroy
ตลอดจนเมื่อมีการประมวลผลเหตุการณ์วงจรอื่นๆ
NavigationView
มีความหมายเหมือนกับที่ใช้กับกิจกรรมหรือ Fragment ของ Android
เช่น onCreate()
ของ NavigationView
จะแปลเป็น
และควรเรียกใช้โดยการเรียกกลับของวงจรชีวิตจากกิจกรรมหรือ Fragment ของ Android เนื่องจากNavigationView
การเรียกกลับของวงจรชีวิตอิงตาม
และเรียกใช้ตามลำดับเดียวกับการเรียกกลับของวงจรชีวิต Android จึงจำเป็นต้องมีการ
จัดลำดับที่เข้มงวดสำหรับเมธอด NavigationView เหล่านี้ มิฉะนั้น คุณอาจพบหน่วยความจำ
รั่ว ข้อผิดพลาดของ UI
ตำแหน่งไม่อัปเดต และปัญหาอื่นๆ
ดูข้อมูลเพิ่มเติมเกี่ยวกับวงจรของกิจกรรม Android ได้ที่ส่วนแนวคิดวงจรของกิจกรรมในเอกสารประกอบสำหรับนักพัฒนาแอป Android
ตารางต่อไปนี้แสดงเวลาที่ควรเรียกใช้เมธอดวงจรอื่นๆ หลังจากเมธอดวงจรที่ระบุ
วิธีการวงจร |
เรียกใช้ที่ใดในวงจรชีวิตของกิจกรรม |
เรียกใช้หลังจากเมธอดวงจรลูกค้าใด |
onConfigurationChanged()
|
เรียกใช้เมื่อ UI อยู่ในเบื้องหน้าและการกำหนดค่ามีการเปลี่ยนแปลง
|
หลังวันที่ onStart() เสมอ
|
onTrimMemory()
|
เรียกใช้เมื่อกิจกรรมอยู่ในเบื้องหลัง
|
หลังวันที่ onPause() เสมอ
|
onSaveInstance()
|
เรียกใช้ก่อนที่จะทำลายกิจกรรม
|
หลัง onStop() เสมอ
|
อย่าเรียกเมธอดวงจรนี้หลายครั้งโดยไม่เรียกเมธอดปิดที่เกี่ยวข้องก่อน นอกจากนี้ โปรดทราบว่าหากแอปจัดการการเรียกกลับวงจรของ Android บางรายการเหล่านี้เอง และมีการเพิ่ม NavigationView
ลงใน Fragment หลังจากสร้างหรือเริ่มต้น แอปควรเรียกใช้เมธอดที่เฉพาะเจาะจงด้วยตนเองตามลำดับที่ถูกต้องเพื่อเริ่มต้นใช้งาน Navigation SDK อย่างถูกต้อง
ดูคำแนะนำเพิ่มเติมเกี่ยวกับการใช้วิธีการเหล่านี้ได้ในแอปเดโม Navigation SDK
หากใช้ NavigationView
ให้เรียกใช้เหตุการณ์วงจรจากกิจกรรมหรือ Fragment อย่างใดอย่างหนึ่ง ไม่ใช่ทั้ง 2 อย่าง
หากต้องการรักษลําดับที่เข้มงวดของเมธอดวงจร ให้เรียกใช้เหตุการณ์เหล่านี้จาก
การเรียกกลับของวงจรกิจกรรมหรือ Fragment ซึ่งจะรับเหตุการณ์เหล่านี้
ตามลําดับ วิธีนี้ช่วยให้มั่นใจได้ว่าแอปไม่จำเป็นต้องประสานงานระหว่าง Fragment และกิจกรรม และทำให้เกิดการเรียกซ้ำ
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\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."]]