# Off-route detection

> **Note (legacy): A newer version of the Navigation SDK is available**
> 
> This page uses v1.6.2 of the Mapbox Navigation SDK. A newer version of the SDK is available. Learn about the latest version, v2.22.2, in the [Navigation SDK documentation](https://docs.mapbox.com/android/navigation/v2/guides/turn-by-turn-navigation/rerouting/).

The Navigation SDK detects when a device strayed from a route it was navigating. When a device is determined to be off route, the SDK provides notifications and takes actions to keep the user navigating.

### Observer

The `OffRouteObserver` interface provides a boolean whenever the Navigation SDK is in turn-by-turn navigation and is off route. When the device is determined to be off route, the Navigation SDK will automatically request a new route.

Create the `OffRouteObserver` interface object:

```kotlin
val offRouteObserver = object : OffRouteObserver {
    override fun onOffRouteStateChanged(offRoute: Boolean) {

    }
}
```

> **Note**
> 
> This interface doesn't work with free-drive mode.

Register the `OffRouteObserver` object with [your already-instantiated `MapboxNavigation` object](https://docs.mapbox.com/android/legacy/navigation/guides/install/#create-a-mapboxnavigation-object).

```kotlin
override fun onStart() {
		super.onStart()
		mapView.onStart()
    mapboxNavigation.registerOffRouteObserver(offRouteObserver)
}
```

Don't forget to unregister the `OffRouteObserver` interface:

```kotlin
override fun onStop() {
	  super.onStop()
  	mapView.onStop()
  	mapboxNavigation.unregisterOffRouteObserver(offRouteObserver)
}
```

### Controller

To customize what conditions trigger off-route conditions, you can provide a custom `RerouteController`:

```kotlin
mapboxNavigation.setRerouteController(object : RerouteController {
    ...
})
```

or to disable off-route detection entirely, pass `null`:

```kotlin
mapboxNavigation.setRerouteController(null)
```

#### Reroute state observer

The controller produces reroute state updates that can be listened to with the `RerouteStateObserver`:

```kotlin
mapboxNavigation.getRerouteController()?.registerRerouteStateObserver(object : RerouteStateObserver {
    ...
})
```