We recently announced Navigation SDK which allows you to seamlessly integrate the familiar Google Maps turn-by-turn navigation experience directly into your Android and iOS applications.
This blog post outlines some of the best practices that you can use to build smooth, consistent and reliable navigation experiences with Navigation SDK for your Android applications.
Interacting with the navigation map
When integrating with the Navigation SDK, you can either use SupportNavigationFragment
, which is a wrapper for NavigationView
, or interact directly with NavigationView
. In most cases, we recommend using SupportNavigationFragment
over NavigationView
. SupportNavigationFragment
handles NavigationView
lifecycle callbacks, so you don’t need to manage them yourself. However, if you do need to interact with NavigationView
, be careful about managing lifecycle methods and events.
Best practices
-
Use
SupportNavigationFragment
instead ofNavigationView
, whenever possible. -
If using
NavigationView
, use strict ordering when invoking lifecycle methods. -
If using
NavigationView
, invoke lifecycle events from the activity or fragment, not both.
For more information, see the Navigation Map interactions in the Navigation SDK developer documentation.
Managing navigation notifications
Navigation notifications are shown when users are navigating, both with or without a destination set, and help ensure location update accuracy. It’s important for apps to manage Android restrictions on starting foreground services from the background and accessing location from the background, in order to maximize location accuracy, minimize battery usage, and avoid memory leaks.
Best practices
-
Request “Allow all the time” location permissions from app users.
-
Ensure proper cleanup of listeners to clear navigation notifications.
-
When re-invoking
NavigationApi#initForegroundServiceManager
methods, invokeNavigationApi#clearForegroundServiceManager
first.
For more information, see the Background location usage in the Navigation SDK developer documentation.
Interacting with the GoogleMap
object
In addition to allowing you to add navigation experiences to your app, the Navigation SDK for Android also includes non-navigation map functionality from the Maps SDK for Android. Non-navigation maps are accessed using the GoogleMap
class. GoogleMap
APIs allow you to create custom maps experiences, including drawing polygons, customizing icons, and more. When implementing GoogleMap
features in your app, it’s important to keep non-navigation maps separate from navigation maps and to manage the GoogleMap
lifecycle in relation to the navigation experience.
Best practices
-
Implement
GoogleMap
features on a separate maps instance from navigation experiences. -
Use
getMapAsync
to obtain theGoogleMap
instance.
For more information, see the GoogleMap interactions in the Navigation SDK developer documentation.
Cleaning up instances
Cleaning up instances and sessions after they are no longer needed is important for avoiding memory leaks and other issues. In particular, be sure to clear navigation destinations and clean up map elements on GoogleMap objects.
Best practices
-
Use
Navigator#clearDestinations
andNavigator#cleanup
when navigation sessions are completed. -
Clean up
GoogleMap
elements when they are no longer needed.
For more information, see the Instance cleanup in the Navigation SDK developer documentation.
Additional recommendations
In addition to the implementation best practices described above, the following recommendations can help ensure that you stay up to date on the latest Navigation SDK developments:
Get started with integration with Navigation SDK. View the Navigation SDK API documentation and references. You can help ensure your users have a smooth and reliable navigation experience by following these best practices.