title: Is it okay to create clicklisteners (or other listeners) inside a viewmodelscope?
body: <p>I have a a fragment containing a googleMap where I am creating a bunch of Markers (which also is clickable). They are spiced with different information (colors, shapes and so on) from a room livedata query. In addition I have some MaterialButton buttons (which are styled as pushbuttons) where I toggle the Marker visible status on. At the moment, the &quot;setup&quot; of theese markers takes some time (200ms-2 secs, depends of amount of markers). To get out of that waiting, I was planning to use a viewmodelscope. Since there are some clicklisteners for theese buttons defined in there (they should do some action with the markers), will they still be alive when the viewmodelscope coroutine section ends, and If they are alive, do they still live in the correct coroutine-context, and do I need to do some housekeeping on the listeners when fragment and/or viewmodel ends?</p>
  <p>I.E:</p>
  <pre><code>class MapsFragment:Fragment(){
  
     private lateinit var mapsViewModel : MapsViewModel
     private lateinit var googleMap : GoogleMap
  
  //...
     override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
  
          mapsViewModel = ViewModelProvider(requireActivity()).get(MapsViewModel::class.java)
  
          _binding = FragmentMapsBinding.inflate(inflater, container, false)
          val root:View = binding.root
  //...
        return root
     }//onCreateView   
  
  //...
  
     override fun onViewCreated(view: View, savedInstanceState:Bundle?){
        super.onViewCreated(view, savedInstanceState)
  //...
  
        mapFragment?.getMapAsync(_googleMap-&gt;
           _googleMap?.let{safeGoogleMap-&gt;
              googleMap = safeGoogleMap
           }?:let{
              Log.e(TAG,&quot;googleMap is null!!&quot;)
              return@getMapAsync
           }   
  //...
        
           mapsViewModel.apply{
  
              liveDataMapsListFromFiltered?.observe(
                 viewLifecycleOwner
              ){mapDetailList-&gt;
                  
                 viewModelScope.launch{ 
  
                  
                    binding.apply{
  
                       //...
                       siteMarkers.map{
                         siteMarker.remove() //removes existing markes from map on update
                       }
                       siteMarkers.clear() //empty the siteMarker array on update 
                       //...
                     
                       mapDetailList?.map{
                          it.apply{
                             //...
                             coordinateSiteLongitude?.let { lng-&gt;
                                coordinateSiteLatitude?.let { lat-&gt;
                                   siteMarkerLatLng = LatLng(lat,lng)
                                   siteLatLngBoundsBuilder?.include(siteMarkerLatLng)
                                }
                             }
                             //...
                             siteMarkerLatLng?.let { safeSiteMarkerLatLng -&gt;
                                val siteMarkerOptions =
                                 MarkerOptions()
                                      .position(safeSiteMarkerLatLng)
                                      .anchor(0.5f, 0.5f)
                                      .visible(siteMarkerState)
                                      .flat(true)
                                    .title(setTicketNumber(ticketNumber?.toDouble()))
                                      .snippet(appointmentName)//TODO: Consider build siteId instead
                                      .icon(siteIcon[iconType])
                                siteMarkers.add(
                                    googleMap.addMarker(siteMarkerOptions) //Here are the markers added
                                )
                             }//siteMarkerLatLng?.let
                           
  
                          }//it.apply
                     
                       }//mapDetailList?.map
  
                       onSiteCheckedChangeListener?.let{
                          fragmentMapsMapTagSelector
                             ?.apTagSelectorMaterialButtonSite
                             ?.removeOnCheckedChangeListener(it) //clearing listener on button before update
                       }
                     
                       onSiteCheckedChangeListener = MaterialButton.OnCheckedChangeListener { siteButton, isChecked -&gt;
                              
                          siteMarkers.map {
                              it.isVisible = isChecked
                          }
                       }.also {
                          fragmentMapsMapTagSelector
                            ?.mapTagSelectorMaterialButtonSite
                            ?.addOnCheckedChangeListener(it)
                       }
  
                       //Will this onCheckedChangeListener still survive when this viewmodelscope runs to the end ?
                     
  
                    }//binding.apply
  
                 }//viewModelScope.launch
  
              }//liveDataMapsListFromFiltered.observe
           
           }//mapsviewModel.apply   
  
  
        }//getMapAsync
     
     }//onViewCreated
  
  }//MapsFragment
  
  </code></pre>