Monday, February 11, 2019

ViewTreeObserver instances might need to be fresh to remove listeners

In an Android app I'm working on, I needed to get notified when layout occurs, so I got a ViewTreeObserver for the relevant view and called addOnGlobalLayoutListener on it. Since I only wanted one notification, I called removeOnGlobalLayoutListener on that observer inside the callback. But I noticed that the callback was called over and over - clearly not removed. When I changed the callback to get a fresh observer from getViewTreeObserver(), though, the remove operation appeared to work.

No comments:

Post a Comment