Android MapView display empty

前端 未结 4 1942
没有蜡笔的小新
没有蜡笔的小新 2021-02-08 13:16

Manifest:




        
4条回答
  •  醉梦人生
    2021-02-08 14:13

    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()
        }
    }
    

提交回复
热议问题