title: How to handle activity lifecycle events for Mapbox Map in Jetpack Compose?
body: <p>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 <a href="https://docs.mapbox.com/android/maps/guides/install/" rel="nofollow noreferrer">here</a>.</p>
  <p>The code mentioned there is like this:</p>
  <pre><code>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 -&gt;
   
      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></pre>
  <p>Code for handling activity and mapbox map lifecycle events is like this:</p>
  <pre><code>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()
  }
  </code></pre>
  <p>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.</p>
  <p>Jetpack compose code snippet for completeness:</p>
  <pre><code>@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 -&gt;
                  Mapbox.getInstance(
                      context,
                      context.getString(R.string.mapbox_access_token)
                  )
                  MapView(context).apply {
                      getMapAsync { mapboxMap -&gt;
                          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)
                  }
          ) {
  
          }
      }
  }
  </code></pre>
  <p>Thank you for your answers and ideas.</p>