-
-
Save skydoves/72c6c7fd75c0a4011d60d1bf271ec0d5 to your computer and use it in GitHub Desktop.
window_layoutinfo
This file contains 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
// Copyright 2022 Google LLC. | |
// SPDX-License-Identifier: Apache-2.0 | |
// | |
// Create a new coroutine since repeatOnLifecycle is a suspend function. | |
lifecycleScope.launch(Dispatchers.Main) { | |
// 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. | |
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { | |
// Safely collect from WindowInfoTracker when the lifecycle is STARTED | |
// and stops collection when the lifecycle is STOPPED | |
WindowInfoTracker.getOrCreate(this@MainActivity) | |
.windowLayoutInfo(this@MainActivity) | |
.collect { layoutInfo -> | |
// do something | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment