I think all data in attrs
gets recycled. In fact in the examples everywhere they recommend to explicitly recycle all obtained typed arrays from the AttributeSet
in constructor of the View. I think you should follow the best practice too.
TypedArray a = context.obtainStyledAttributes(attrs, set);
try {
// read attributes from a
// ...
} finally {
attributes.recycle();
}
My guess is that when onFinishInflate()
is called, attrs
is already empty or unavailable.
In fact, according to the code of LayoutInflater
, the AttributeSet
that is passed into View constructor during layout inflation is actually based on XmlPullParser
. The onFinishInflate()
is called on the parent view after inflating all of its children.
It looks like at this point in 'onFinishInflate()' you cannot obtain the values of your attribute from the attrs
. Especially if you have children inside your layout - the attrs
will reference to the same parser, but the parser's position will point to the attributes of the last parsed child.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…