0

I have looked online for an answer to this, but have not found the root cause of this issue.

I have an application with a view, that listens in on onVisibilityChanged and onWindowVisibilityChanged. What I have noticed is that on Android devices with either KitKat (API 19) or Lollipop (API 22), when the view gets attached to the main layout, onVisibilityChanged is never fired, only onWindowVisibilityChanged.

I am guessing this issue originates with a native API change that occurred from KitKat up to Lollipop, but have not found any documentation or reference to this. When I checked my application on Marshmallow (API 23), it did not happen.

I would just like to know if this is a known issue or if there is some sort of way to fix this behavior.

Thanks.

14
  • post your code snippet Commented Dec 1, 2018 at 9:06
  • @PembaTamang - Cannot post code snippet as it is intricate code with company logic. In respect to the View classes, the two listeners, onVisibilityChanged and onWindowVisibilityChanged are implemented as is. They should be called normally (as happens in OS versions after 5.1). Commented Dec 1, 2018 at 16:53
  • could you de couple the business logic and post the code ?? Commented Dec 2, 2018 at 14:48
  • its the only way you'll find a good answer Commented Dec 2, 2018 at 14:49
  • @PembaTamang - I understand what you are saying, but in regards to the visibility listeners, they are implemented vanilla wise. There is no special logic there. Just overriding the methods from the parent view class. There should not be an issue if they are working on newer operating system versions. Commented Dec 2, 2018 at 15:26

1 Answer 1

2
+50

It looks like the Android team did indeed change this behavior in Android 23 - Marshmallow. If you look at the source code for View.java in Android 23, you can see that they added a call to onVisibilityChanged inside of dispatchAttachedToWindow.

Here is the commit to AOSP where this change happened.

Here is the diff for that change.

Given that you are unable to post your code, I cannot offer a better solution than to point you to the change.

If you are trying to track view attaches to the window, I would suggest the onAttachedToWindow API.

Note that the method that calls onVisibilityChanged in Android 23 is most likely dispatchAttachedToWindow. This method also calls onAttachedToWindow, so you could theoretically listen for this and check the visibility manually.

   // Override inside your custom view
   override fun onAttachedToWindow() {
        super.onAttachedToWindow()

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            // Check for visibility here, but note that onVisibilityChanged initially gets called with View.GONE
            // Unless the view is inside of a ViewGroup. See the source code for ViewRootImpl.java for more details.
        }
    }

Otherwise, you will need to special case your logic for Android 22 and below such that you listen to the onWindowVisibilityChanged more instead of onVisibilityChanged.

1
  • Thank you very much. This is what I was suspicious of. Major kudos! Commented Dec 5, 2018 at 6:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.