Currently, requestApplyInsets is called once when the view is attached to a window, via doOnAttach (either immediately or the next time the view is attached):
This works well for most cases, but there are corner cases where this isn't enough.
For instance, suppose a view within a ViewHolder within a RecyclerView is has left/right insets applied to it from the systemBars insets. The consider the following sequence of events:
The device is put in landscape orientation, with 3-button navigation (so the left/right insets are non-zero)
The user scrolls the RecyclerView, so that some ViewHolders that have insets applied are detached from the window
The user directly rotates the device 180 degrees. Since this is still landscape, no recreation of views occurs, but new insets are dispatched.
The user scrolls the RecyclerView so that the previously detached ViewHolders are reattached to the window.
Since the new insets were dispatched while the ViewHolders were detached, these ViewHolders will now be incorrectly displaying the old insets, and since they were already attached once, requestApplyInsets won't be called again.
The following video shows an example of that case:
My proposed fix is to replace doOnAttach with doOnEveryAttach, which will run the action immediately if the view is already attached, and then will run the action again upon every subsequent attach.
Currently,
requestApplyInsets
is called once when the view is attached to a window, viadoOnAttach
(either immediately or the next time the view is attached):https://github.com/chrisbanes/insetter/blob/9b1eed571aa8c5c8112f1aa6c3cb5d57a1ab4dd8/library/src/main/java/dev/chrisbanes/insetter/Insetter.kt#L394-L396
This works well for most cases, but there are corner cases where this isn't enough.
For instance, suppose a view within a
ViewHolder
within aRecyclerView
is has left/right insets applied to it from thesystemBars
insets. The consider the following sequence of events:RecyclerView
, so that someViewHolder
s that have insets applied are detached from the windowRecyclerView
so that the previously detachedViewHolder
s are reattached to the window.Since the new insets were dispatched while the
ViewHolder
s were detached, theseViewHolder
s will now be incorrectly displaying the old insets, and since they were already attached once,requestApplyInsets
won't be called again.The following video shows an example of that case:
https://user-images.githubusercontent.com/4217560/105615109-abde9980-5d93-11eb-98e1-6aa5dc332def.mp4
My proposed fix is to replace
doOnAttach
withdoOnEveryAttach
, which will run the action immediately if the view is already attached, and then will run the action again upon every subsequent attach.