美文网首页
Android-Jetpack Compose 如何处理Life

Android-Jetpack Compose 如何处理Life

作者: Cosecant | 来源:发表于2022-03-21 16:45 被阅读0次

    下面是一段来自谷歌案例代码中的一部分,希望对你有用。原文摘取于:android - How to handle activity lifecycle events for Mapbox Map in Jetpack Compose? - Stack Overflow

    @Composable
    fun MapWrapper() {
        ConstraintLayout(modifier = Modifier.fillMaxSize()) {
            val mapboxMap = createRef()
            val fab = createRef()
            val mapView = rememberMapViewWithLifecycle()
    
    
            AndroidView(
                factory = {mapView},
                modifier = Modifier.constrainAs(mapboxMap) {
                    top.linkTo(parent.top)
                    bottom.linkTo(parent.bottom)
                    start.linkTo(parent.start)
                    end.linkTo(parent.end)
            })
    
            FloatingActionButton(
                onClick = {
    
                },
                modifier = Modifier
                    .padding(25.dp)
                    .width(50.dp)
                    .height(50.dp)
                    .constrainAs(fab) {
                        end.linkTo(mapboxMap.end)
                        bottom.linkTo(mapboxMap.bottom)
                    }
            ) {
    
            }
        }
    }
    
    @Composable
    fun rememberMapViewWithLifecycle(): MapView {
        val context = LocalContext.current
        val mapView = remember {
            Mapbox.getInstance(
                context,
                context.getString(R.string.mapbox_access_token)
            )
            MapView(context).apply {
                val mapView = this
                getMapAsync { mapboxMap ->
                    mapboxMap.setStyle(Style.MAPBOX_STREETS)
    
                    val position = CameraPosition.Builder()
                        .target(LatLng(70.04004014308637, -20.744085852141072))
                        .zoom(15.0)
                        .build()
    
                    mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), 1)
    
                    mapboxMap.getStyle {
                    }
                }
            }
        }
    
        val lifecycle = LocalLifecycleOwner.current.lifecycle
        DisposableEffect(lifecycle, mapView) {
            // Make MapView follow the current lifecycle
            val lifecycleObserver = getMapLifecycleObserver(mapView)
            lifecycle.addObserver(lifecycleObserver)
            onDispose {
                lifecycle.removeObserver(lifecycleObserver)
            }
        }
    
        return mapView
    }
    
    /**
     * Handles lifecycle of provided mapView
     */
    private fun getMapLifecycleObserver(mapView: MapView): LifecycleEventObserver =
        LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_CREATE -> mapView.onCreate(Bundle())
                Lifecycle.Event.ON_START -> mapView.onStart()
                Lifecycle.Event.ON_RESUME -> mapView.onResume()
                Lifecycle.Event.ON_PAUSE -> mapView.onPause()
                Lifecycle.Event.ON_STOP -> mapView.onStop()
                Lifecycle.Event.ON_DESTROY -> mapView.onDestroy()
                else -> throw IllegalStateException()
            }
        }
    

    相关文章

      网友评论

          本文标题:Android-Jetpack Compose 如何处理Life

          本文链接:https://www.haomeiwen.com/subject/gkjtjrtx.html