แนวทางปฏิบัติแนะนำสำหรับการล้างข้อมูลอินสแตนซ์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หน้านี้อธิบายแนวทางปฏิบัติแนะนำในการล้างข้อมูลอินสแตนซ์หลังจากที่ไม่มีความจำเป็นอีกต่อไป
ใช้ Navigator#clearDestinations
และ Navigator#cleanup
เมื่อเซสชันการนำทางเสร็จสมบูรณ์
หากต้องการป้องกันหน่วยความจำรั่วและตรวจสอบว่ามีการล้างข้อมูลอย่างถูกต้อง ให้ใช้
Navigator#clearDestinations
และ Navigator#cleanup
หลังจากเซสชันการนำทาง
เสร็จสมบูรณ์และไม่จำเป็นต้องใช้อินสแตนซ์ Navigator อีกต่อไป ซึ่งจะทำได้เมื่อผู้ใช้ออกจากแผนที่และเมื่อมีการเรียกใช้ onDestroy()
ของกิจกรรมที่เชื่อมโยง
ล้างองค์ประกอบ GoogleMap
เมื่อไม่ต้องการใช้แล้ว
หากแอปใช้GoogleMap
อินสแตนซ์สำหรับประสบการณ์การใช้งานแผนที่ที่ไม่ใช่การนำทาง โปรดล้างข้อมูลอินสแตนซ์นี้เมื่อไม่จำเป็นต้องใช้แล้ว เช่น ล้างองค์ประกอบ Polygon, Polyline และ Marker ที่เชื่อมโยงกับ GoogleMap
โดยเรียกใช้เมธอด Polygon#remove
, Polyline#remove
และ Marker#remove
ตามลำดับ
จากนั้นหากไม่ต้องการใช้GoogleMap
อินสแตนซ์อีกต่อไป ให้เรียกใช้ GoogleMap#clear
เพื่อกำหนดอินสแตนซ์ให้กับ null
ดูข้อมูลเพิ่มเติมเกี่ยวกับการใช้อินสแตนซ์ GoogleMap
ในแอปได้ที่แนวทางปฏิบัติแนะนำ
GoogleMap
ในการโต้ตอบ
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eUse \u003ccode\u003eNavigator#clearDestinations\u003c/code\u003e and \u003ccode\u003eNavigator#cleanup\u003c/code\u003e when navigation sessions are completed to prevent memory leaks.\u003c/p\u003e\n"],["\u003cp\u003eClean up \u003ccode\u003eGoogleMap\u003c/code\u003e elements like Polygons, Polylines, and Markers by calling their respective \u003ccode\u003eremove\u003c/code\u003e methods when they are no longer needed.\u003c/p\u003e\n"],["\u003cp\u003eAfter a \u003ccode\u003eGoogleMap\u003c/code\u003e instance is no longer in use, call \u003ccode\u003eGoogleMap#clear\u003c/code\u003e and assign the instance to \u003ccode\u003enull\u003c/code\u003e to free up resources.\u003c/p\u003e\n"]]],[],null,["# Instance cleanup best practices\n\nThis page explains best practices for cleaning up instances after they are no\nlonger needed.\n\nUse `Navigator#clearDestinations` and `Navigator#cleanup` when navigation sessions are completed\n------------------------------------------------------------------------------------------------\n\nTo prevent memory leaks and ensure proper cleanup, use\n`Navigator#clearDestinations` and `Navigator#cleanup` after the navigation\nsession is completed and the Navigator instance is no longer needed. This can be\ndone when the user has navigated away from the map and when `onDestroy()` of the\nassociated activity is invoked.\n\nClean up `GoogleMap` elements when they are no longer needed\n------------------------------------------------------------\n\nIf your app uses a `GoogleMap` instance for non-navigation maps experiences, be\nsure to clean up this instance when it is no longer needed. For example, clean\nup the Polygon, Polyline, and Marker elements associated with `GoogleMap` by\ncalling the `Polygon#remove`, `Polyline#remove`, and `Marker#remove` methods,\nrespectively. Then, to free up the `GoogleMap` instance after it is not longer\nneeded, call `GoogleMap#clear` assign the instance to `null`.\n\nFor more information about using a `GoogleMap` instance in your app, see\n[`GoogleMap` interaction best\npractices](/maps/documentation/navigation/android-sdk/googlemap-interactions-best-practices)."]]