Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Last active November 4, 2021 17:54
Show Gist options
  • Select an option

  • Save manuelvicnt/88056d27f4af2be5656025198edae30f to your computer and use it in GitHub Desktop.

Select an option

Save manuelvicnt/88056d27f4af2be5656025198edae30f to your computer and use it in GitHub Desktop.
class LocationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Create a new coroutine since repeatOnLifecycle is a suspend function
lifecycleScope.launch {
// The block passed to repeatOnLifecycle is executed when the lifecycle
// is at least STARTED and is cancelled when the lifecycle is STOPPED.
// It automatically restarts the block when the lifecycle is STARTED again.
repeatOnLifecycle(Lifecycle.State.STARTED) {
// Safely collect from locationFlow when the lifecycle is STARTED
// and stops collection when the lifecycle is STOPPED
locationProvider.locationFlow().collect {
// New location! Update the map
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment