Skip to content

Instantly share code, notes, and snippets.

@manuelvicnt
Created March 23, 2021 07:14
Show Gist options
  • Save manuelvicnt/e9b4c5fe496b8d9020333f6856bf9b4a to your computer and use it in GitHub Desktop.
Save manuelvicnt/e9b4c5fe496b8d9020333f6856bf9b4a to your computer and use it in GitHub Desktop.
class LocationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Collects from the flow when the View is at least STARTED and
// SUSPENDS the collection when the lifecycle is STOPPED.
// Collecting the flow cancels when the View is DESTROYED.
lifecycleScope.launchWhenStarted {
locationProvider.locationFlow().collect {
// New location! Update the map
}
}
// Same issue with:
// - lifecycleScope.launch { /* Collect from locationFlow() here */ }
// - locationProvider.locationFlow().onEach { /* ... */ }.launchIn(lifecycleScope)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment