Package 

Class ViewWindowInsetObserver


  • @Deprecated(message = 
    accompanist/insets is deprecated.
    ViewWindowInsetObserver is not necessary in androidx.compose and can be removed.
    For more migration information, please visit https://google.github.io/accompanist/insets/#migration
    ) 
    public final class ViewWindowInsetObserver
    
                        

    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 */
                }
            }
        }
    • Constructor Detail

      • ViewWindowInsetObserver

        ViewWindowInsetObserver(View view)
        Parameters:
        view - The view to observe WindowInsetsCompats from.
    • Method Detail

      • start

         final WindowInsets start(Boolean consumeWindowInsets, Boolean windowInsetsAnimationsEnabled)

        Start observing window insets from view. Make sure to call stop if required.

        Parameters:
        consumeWindowInsets - Whether to consume any WindowInsetsCompats which are dispatched to the host view.
        windowInsetsAnimationsEnabled - Whether to listen for WindowInsetsAnimations, such as IME animations.
      • stop

         final Unit stop()

        Removes any listeners from the view so that we no longer observe inset changes.

        This is only required to be called from hosts which have a shorter lifetime than the view. For example, if you're using ViewWindowInsetObserver from a @Composable function, you should call stop from an onDispose block, like so:

        DisposableEffect(view) {
            val observer = ViewWindowInsetObserver(view)
            // ...
            onDispose {
                observer.stop()
            }
        }

        Whereas if you're using this class from a fragment (or similar), it is not required to call this function since it will live as least as longer as the view.