in the official documentation for Mapbox SDK for android there is a simple example of map using standard UI library for Android. Full documentation for completeness of the question can be found here.
The code mentioned there is like this:
private var mapView: MapView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Mapbox.getInstance(this, getString(R.string.mapbox_access_token))
setContentView(R.layout.activity_main)
mapView = findViewById(R.id.mapView)
mapView?.onCreate(savedInstanceState)
mapView?.getMapAsync { mapboxMap ->
mapboxMap.setStyle(Style.MAPBOX_STREETS) {
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
}
}
}
Code for handling activity and mapbox map lifecycle events is like this:
override fun onStart() {
super.onStart()
mapView?.onStart()
}
override fun onResume() {
super.onResume()
mapView?.onResume()
}
override fun onPause() {
super.onPause()
mapView?.onPause()
}
override fun onStop() {
super.onStop()
mapView?.onStop()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView?.onSaveInstanceState(outState)
}
override fun onLowMemory() {
super.onLowMemory()
mapView?.onLowMemory()
}
override fun onDestroy() {
super.onDestroy()
mapView?.onDestroy()
}
My question is, if one is using mapbox map in jetpack compose, are those events of the map dealt with automatically, or does developer need to handle them himself? I am asking, because I do not want to have any doubts how to deal with the best practices regarding the AndroidView in jetpack compose especially when the map is placed in composable outside of any particular activity or fragment.
Jetpack compose code snippet for completeness:
@Composable
fun MapWithFab() {
ConstraintLayout(modifier = Modifier.fillMaxSize()) {
val mapboxMap = createRef()
val fab = createRef()
AndroidView(
modifier = Modifier
.fillMaxSize()
.padding(bottom = 35.dp)
.constrainAs(mapboxMap) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
bottom.linkTo(parent.bottom)
},
factory = { context ->
Mapbox.getInstance(
context,
context.getString(R.string.mapbox_access_token)
)
MapView(context).apply {
getMapAsync { mapboxMap ->
mapboxMap.setStyle(Style.MAPBOX_STREETS)
val position = CameraPosition.Builder()
.target(LatLng(51.04004014308637, 13.744085852141072))
.zoom(15.0)
.build()
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), 1)
}
}
}
)
FloatingActionButton(
onClick = { },
modifier = Modifier
.padding(25.dp)
.width(50.dp)
.height(50.dp)
.constrainAs(fab) {
end.linkTo(mapboxMap.end)
bottom.linkTo(mapboxMap.bottom)
}
) {
}
}
}
Thank you for your answers and ideas.