Navigation SDK for Android
Beta

Trips

The Mapbox Navigation SDK's internal logic runs various calculations, no matter whether it's for turn-by-turn navigation or free-drive mode. It manages, filters, and enhances location updates. It computes precise route progress data when navigating. It has many observers and you can receive callbacks that deliver various types of information.

There are times when you might want to plainly know when this logic has started or stopped. When it changes, perhaps you'll make Location listening updates, change an Android-system View's visibility, or move the MapboxMap's camera.

This page explains how you can be notified of the start or stop state so that you can make the changes that you want.

Managing the session

To take advantage of the Navigation SDK's observers and logic, you need to request the location permissions and start the trip session:

mapboxNavigation.startTripSession()

When started, the session will create and run a Foreground Android Service, ensuring a stable stream of raw location updates to process, even when the app is in the background.

To pause the stream of navigation events and stop the Foreground Service, call:

mapboxNavigation.stopTripSession()

Trip session events

While the session is started and actively processing raw location updates, it returns the computed data using multiple observers:

Observe TripSessionState

The TripSessionStateObserver observer interface provides a TripSessionState object whenever the state has changed.

The Navigation SDK's TripSessionState enum class provides the two possible states:

  • STARTED – State when the session is active. An active session runs a foreground service. Location updates are continually requested and returned to the Navigation SDK.
  • STOPPED – State when the session is inactive.

To observe the session's state, create the interface object:

val tripSessionStateObserver = object : TripSessionStateObserver {

    override fun onSessionStateChanged(tripSessionState: TripSessionState) {
        
        when (tripSessionState) {
            
            TripSessionState.STARTED -> {
                
            }
            
            TripSessionState.STOPPED -> {
            
            }
        }
    }
}

Register the interface object with your already-instantiated MapboxNavigation object:

mapboxNavigation.registerTripSessionStateObserver(tripSessionStateObserver)

Don’t forget to unregister the TripSessionStateObserver interface!:

override fun onStop() {
    super.onStop()  
    mapView.onStop()
        
    mapboxNavigation.unregisterTripSessionStateObserver(tripSessionStateObserver)}
}

The unregistering above is automatically done for you if you run mapboxNavigation.onDestroy().

Was this page helpful?