View Window Inset Observer
This class sets up the necessary listeners on the given view to be able to observe WindowInsetsCompat instances dispatched by the system.
This class is useful for when you prefer to handle the ownership of the WindowInsets yourself. One example of this is if you find yourself using ProvideWindowInsets in fragments.
It is convenient to use ProvideWindowInsets in fragments, but that can result in a delay in the initial inset update, which results in a visual flicker. See this issue for more information.
The alternative is for fragments to manage the WindowInsets themselves, like so:
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = ComposeView(requireContext()).apply {
layoutParams = LayoutParams(MATCH_PARENT, MATCH_PARENT)
// Create an ViewWindowInsetObserver using this view
val observer = ViewWindowInsetObserver(this)
// Call start() to start listening now.
// The WindowInsets instance is returned to us.
val windowInsets = observer.start()
setContent {
// Instead of calling ProvideWindowInsets, we use CompositionLocalProvider to provide
// the WindowInsets instance from above to LocalWindowInsets
CompositionLocalProvider(LocalWindowInsets provides windowInsets) {
/* Content */
}
}
}
Parameters
view
The view to observe WindowInsetsCompats from.
Constructors
ViewWindowInsetObserver
Link copied to clipboard
The view to observe WindowInsetsCompats from.
Functions
start
Link copied to clipboard
fun start(consumeWindowInsets: Boolean = true, windowInsetsAnimationsEnabled: Boolean = true): WindowInsets
Content copied to clipboard
Properties
isObserving
Link copied to clipboard
Whether this ViewWindowInsetObserver is currently observing.