Last active
November 4, 2021 17:54
-
-
Save manuelvicnt/88056d27f4af2be5656025198edae30f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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