Skip to content

Instantly share code, notes, and snippets.

@skydoves
Created January 20, 2022 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skydoves/72c6c7fd75c0a4011d60d1bf271ec0d5 to your computer and use it in GitHub Desktop.
Save skydoves/72c6c7fd75c0a4011d60d1bf271ec0d5 to your computer and use it in GitHub Desktop.
window_layoutinfo
// 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