Các phương pháp hay nhất để dọn dẹp thực thể
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Trang này trình bày các phương pháp hay nhất để dọn dẹp các phiên bản sau khi không còn cần thiết.
Sử dụng Navigator#clearDestinations
và Navigator#cleanup
khi phiên điều hướng hoàn tất
Để ngăn chặn tình trạng rò rỉ bộ nhớ và đảm bảo dọn dẹp đúng cách, hãy sử dụng Navigator#clearDestinations
và Navigator#cleanup
sau khi phiên điều hướng hoàn tất và không cần đến phiên bản Navigator nữa. Bạn có thể thực hiện việc này khi người dùng đã rời khỏi bản đồ và khi onDestroy()
của hoạt động được liên kết được gọi.
Dọn dẹp các phần tử GoogleMap
khi không còn cần thiết nữa
Nếu ứng dụng của bạn sử dụng một thực thể GoogleMap
cho các trải nghiệm không phải là bản đồ chỉ đường, hãy nhớ dọn dẹp thực thể này khi không còn cần đến. Ví dụ: dọn dẹp các phần tử Đa giác, Đường nhiều đoạn và Điểm đánh dấu được liên kết với GoogleMap
bằng cách gọi các phương thức Polygon#remove
, Polyline#remove
và Marker#remove
tương ứng. Sau đó, để giải phóng phiên bản GoogleMap
sau khi không cần đến nữa, hãy gọi GoogleMap#clear
và chỉ định phiên bản đó cho null
.
Để biết thêm thông tin về cách sử dụng một thực thể GoogleMap
trong ứng dụng, hãy xem phần Các phương pháp hay nhất về hoạt động tương tác GoogleMap
.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 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)."]]