Sprawdzone metody czyszczenia instancji
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Na tej stronie znajdziesz sprawdzone metody usuwania instancji, które nie są już potrzebne.
Używaj przycisków Navigator#clearDestinations
i Navigator#cleanup
po zakończeniu sesji nawigacji
Aby zapobiec wyciekom pamięci i zapewnić prawidłowe czyszczenie, użyj Navigator#clearDestinations
i Navigator#cleanup
po zakończeniu sesji nawigacji i gdy instancja Navigator nie będzie już potrzebna. Można to zrobić, gdy użytkownik opuści mapę i wywoła onDestroy()
powiązanej aktywności.
Usuwanie elementów GoogleMap
, gdy nie są już potrzebne
Jeśli aplikacja używa instancji GoogleMap
do wyświetlania map w innych celach niż nawigacja, pamiętaj, aby usunąć tę instancję, gdy nie będzie już potrzebna. Na przykład wyczyść elementy wielokąta, polilinii i markera powiązane z GoogleMap
, wywołując odpowiednio metody Polygon#remove
, Polyline#remove
i Marker#remove
. Następnie, aby zwolnić instancję GoogleMap
, gdy nie będzie już potrzebna, wywołaj GoogleMap#clear
i przypisz instancję do null
.
Więcej informacji o korzystaniu z instancji GoogleMap
w aplikacji znajdziesz w artykule GoogleMap
– sprawdzone metody interakcji.
O ile nie stwierdzono inaczej, treść tej strony jest objęta licencją Creative Commons – uznanie autorstwa 4.0, a fragmenty kodu są dostępne na licencji Apache 2.0. Szczegółowe informacje na ten temat zawierają zasady dotyczące witryny Google Developers. Java jest zastrzeżonym znakiem towarowym firmy Oracle i jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-08-31 UTC.
[null,null,["Ostatnia aktualizacja: 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)."]]