Saving and resuming navigation
Whether the app crashes, is accidentally terminated, or the system reclaims resources, saving an ongoing navigation session allows TomTomNavigation to restore its previous state when the app is restarted.
To support session restoration, TomTomNavigation provides the TomTomNavigation.navigationResumeSnapshot property and the TomTomNavigation.resume(navigationResumeSnapshot: NavigationResumeSnapshot) method. To serialize a NavigationResumeSnapshot , use the NavigationResumeSnapshot.serializeToFile(snapshotFle: File) or NavigationResumeSnapshot.serializeToBytes() methods. To deserialize it, use the NavigationResumeSnapshot.deserializeFromFile(file: File) or NavigationResumeSnapshot.deserializeFromBytes(data: byteArray) method.
You can save and resume a navigation session either automatically or manually.
Saving and resuming a navigation session automatically
To start saving navigation data automatically, create a default implementation of NavigationResumeSnapshotRenewer . Once navigation starts, a snapshot is saved periodically—every 30 seconds by default.
val navigationResumeSnapshotRenewer = NavigationResumeSnapshotRenewerFactory.create( context = context, options = NavigationResumeSnapshotRenewerOptions(), tomTomNavigation = tomTomNavigation, )The automatic saving interval is configured through NavigationResumeSnapshotRenewerOptions as shown below:
val options = NavigationResumeSnapshotRenewerOptions(saveInterval = 40.seconds)val navigationResumeSnapshotRenewer = NavigationResumeSnapshotRenewerFactory.create( context = context, options = options, tomTomNavigation = tomTomNavigation, )To automatically resume a previously saved session, use the NavigationResumeSnapshotRenewer.resumeNavigation() method:
navigationResumeSnapshotRenewer.resumeNavigation( callback = object : Callback<NavigationResumeSnapshot, NavigationResumeSnapshotRenewerFailure> { override fun onSuccess(result: NavigationResumeSnapshot) { // YOUR CODE GOES HERE }
override fun onFailure(failure: NavigationResumeSnapshotRenewerFailure) { // YOUR CODE GOES HERE } },)To resume a previously saved session manually, use the NavigationResumeSnapshotRenewer.latestNavigationResumeSnapshot() method:
navigationResumeSnapshotRenewer.latestNavigationResumeSnapshot( callback = object : Callback<NavigationResumeSnapshot, NavigationResumeSnapshotRenewerFailure> { override fun onSuccess(result: NavigationResumeSnapshot) { tomTomNavigation.resume(result) // YOUR CODE GOES HERE }
override fun onFailure(failure: NavigationResumeSnapshotRenewerFailure) { // YOUR CODE GOES HERE } },)Saving and resuming a navigation session manually
To manually save a NavigationResumeSnapshot to a file, use the following snippet:
val fileName = "navigation_snapshot"tomTomNavigation.navigationResumeSnapshot() .ifSuccess { snapshot -> snapshot.serializeToBytes() // or snapshot.serializeToFile(file) .ifSuccess { data -> val file = File(context.filesDir, fileName) FileOutputStream(file).use { fileOutputStream -> fileOutputStream.write(data) } } .ifFailure { failure: NavigationResumeSnapshotSerializationFailure -> // YOUR CODE GOES HERE } } .ifFailure { failure: NavigationResumeSnapshotFailure -> // YOUR CODE GOES HERE }To resume a previously saved session from a file, use the following snippet:
val fileName = "navigation_snapshot"val file = File(context.filesDir, fileName)if (file.exists()) { NavigationResumeSnapshot.deserializeFromFile(file) .ifSuccess { snapshot -> tomTomNavigation.resume(navigationResumeSnapshot = snapshot) } .ifFailure { failure: NavigationResumeSnapshotSerializationFailure -> // YOUR CODE GOES HERE }}To improve performance, save the NavigationResumeSnapshot after key events that result in significant changes to the navigation state, such as:
- Starting navigation along a route
- Refreshing the route
- Deviating from the route
- Visiting a waypoint