Per the documentation (as of today) you need to forward lifecycle callbacks to the MapView.
A cleaner way to do this is to use LifecycleObserver.
This works when the map is placed in an activity or fragment.
You don't get onLowMemory()
or onSaveInstanceState()
but you could still forward those to the MapView.
Here's a Kotlin example.
import android.content.Context
import android.util.AttributeSet
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import com.google.android.gms.maps.MapView
class MyMapView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : MapView(context, attrs, defStyle), LifecycleObserver {
init {
if(context is LifecycleOwner) {
context.lifecycle.addObserver(this)
}
}
//Your code here
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun create() {
onCreate(null)
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun start() {
onStart()
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun resume() {
onResume()
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun pause() {
onPause()
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stop() {
onStop()
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun destroy() {
onDestroy()
}
}