开发者

Difference between Preference's onCreateView and onBindView methods

开发者 https://www.devze.com 2023-04-03 06:48 出处:网络
What is the difference between onCreateView and onBindView methods in Preference? In documentation it says that onBindView:

What is the difference between onCreateView and onBindView methods in Preference?

In documentation it says that onBindView:

Binds the 开发者_C百科created View to the data for this Preference. This is a good place to grab references to custom Views in the layout and set properties on them.

Why is it such a good place to set properties on Views in my layout? Currently I am setting properties in onCreateView method and everything seems to work fine. From my experience it looks like both methods are always called together. Maybe there are some situations when only onBindView is called?


onCreateView() is for creating the View hierarchy that will eventually contain the Preference UI. onBindView() is for binding actual data to that View hierarchy created in onCreateView().

The pattern separates the creation of the View hierarchy - which is cached - from the binding of data to that View hierarchy. In the case of Preference, onCreateView() is only called once, but onBindView() is called every time the UI needs to load the Preference View.

I am guessing that your current setup works because you never change the properties that you set on the Preference. It would be better to setup the properties of the View hierarchy in onBindView(), in case it ever needs to be dynamic.

(As an aside, this View creation vs. binding design pattern is also seen in CursorAdapters, where it only creates enough Views to display on screen, but is constantly binding these Views to new data.)

0

精彩评论

暂无评论...
验证码 换一张
取 消