Reputation: 6460
I'm not sure about the usage of onAttachedToWindow
.
My question is basically about the documentation. Which mentions:
[...] it may be called any time before the first onDraw -- including before or after onMeasure(int, int)
I what to know:
when it is called before onMeasure
and when is it called after.
The Story behind:
I am adding OnGlobalLayoutListener
in onAttachedToWindow
and remove it in onDetachedFromWindow
. Because is somehow logic to me to handle the layouting when the view is added to the window.
But I am concerned that the first onGlobalLayout calls get lost, if the Listener is not added yet. (because onMeasure usually happens during the layouting)
If someone got a better approach for my problem, feel free to give me a hint.
Upvotes: 4
Views: 5405
Reputation: 29436
Docs are correct, and you should not rely on onWindowAttach/Detach
being in sync with onMeasure
or onLayout
pass.
If your View
class is interested in parent hierarchy changes, I'd advice against such a design. The Parent UI should notify sub-views of hierarchy changes. So, OnGlobalLayoutListener
better be used by an enclosing UI class.
Also, View
class has onSizeChanged()
that you can override to detect when it has been measured up.
Upvotes: 4