Who/What calls onFinishInflate()? No matter how I inflate my layout files (in code) this method never seems to get triggered. Can anyone give me an example or tell me when onFinishInflate() actually get called?
View.onFinishInflate()
is called after a view (and its children) is inflated from XML. Specifically, it is during a call to LayoutInflater.inflate(...)
that onFinishInflate()
will be called. Inflation is performed recursively, starting from the root. A view containing children may need to know when its children have finished being inflated. One of the main uses of this callback is for ViewGroups
to perform special actions once its children are ready.
Let's assume you had a subclass of View
called CustomView
, and that it does not internally inflate any layouts itself. If you had a CustomView
somewhere in your layout, i.e. :
...
<com.company.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
...
You should see a callback to onFinishInflate()
once it has been inflated. If this is in your main layout of your activity, then you can consider this being after Activity.setContentView(int)
is called. Internally, this will call LayoutInflater.inflate(...)
.
Alternatively, if you created an instance of CustomView
with:
...
new CustomView(context);
...
...you will not get a call to onFinishInflate()
. Instantiating it in this way will naturally mean it has no children and hence it does not have to wait for them to be instantiated in the recursive fashion, as in XML inflation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With