ViewWindowInsetObserver

class ViewWindowInsetObserver(view: View)

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
fun ViewWindowInsetObserver(view: View)
The view to observe WindowInsetsCompats from.

Functions

equals
Link copied to clipboard
open operator fun equals(other: Any?): Boolean
hashCode
Link copied to clipboard
open fun hashCode(): Int
start
Link copied to clipboard
fun start(consumeWindowInsets: Boolean = true): WindowInsets
Start observing window insets from view.
fun start(windowInsetsAnimationsEnabled: Boolean, consumeWindowInsets: Boolean = true): WindowInsets
Start observing window insets from view.
stop
Link copied to clipboard
fun stop()
Removes any listeners from the view so that we no longer observe inset changes.
toString
Link copied to clipboard
open fun toString(): String

Properties

isObserving
Link copied to clipboard
var isObserving: Boolean = false
Whether this ViewWindowInsetObserver is currently observing.

Sources

(source)
Link copied to clipboard