Skip to main content

Initialization / Release

SDK lifecycle​

FusedLocation must be initialized and released according to the application lifecycle.

StageAPIDescription
Check environment supportisSupported()Check whether the current environment supports the FusedLocation SDK.
Initializationinitialize()Create a FusedLocation instance and initialize the SDK.
Receiving location datagetLastKnownLocation(), registerFusedLocationCallback()After calling initialize(), refer to the Location Data Access guide to retrieve location data or receive location updates as needed. Check the permissions required for each API as well.
Stop receiving location updatesunregisterFusedLocationCallback()Unregister the listener when location updates are no longer needed.
Resource releaserelease()Release the SDK resources when the SDK is no longer needed.

If you call initialize(), you must call release() when you are finished using the SDK. unregisterFusedLocationCallback() unregisters the location update listener, while release() releases the SDK itself; they cannot be used interchangeably.

Initialization and release example​

The following example manages a single FusedLocation instance at the application scope.

class MyApplication : Application() {
lateinit var fusedLocation: FusedLocation
private set

override fun onCreate() {
super.onCreate()

// 1. Create an instance when the application starts.
fusedLocation = FusedLocation(this)

// 2. Check support status before initializing the SDK.
if (fusedLocation.isSupported()) {
fusedLocation.initialize()
} else {
// Disable location-related UI and features.
}
}

fun shutdownFusedLocation(listener: FusedLocationListener) {
// 3. Unregister the registered location update listener first.
fusedLocation.unregisterFusedLocationCallback(listener)

// 4. Release the SDK resources when the SDK is no longer needed.
fusedLocation.release()
}
}

You can use the location data APIs after calling initialize(). If you registered location updates, call unregisterFusedLocationCallback() to unregister the listener before calling release(). After calling release(), do not call FusedLocation APIs on the same instance. If you need to use the SDK again, create a new instance, check its support status, and initialize it.