Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onFinishInflate() never gets called

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?

like image 660
user123321 Avatar asked Mar 10 '12 22:03

user123321


1 Answers

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.

like image 86
antonyt Avatar answered Nov 01 '22 11:11

antonyt