Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Preference's onCreateView and onBindView methods

Tags:

android

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

In documentation it says that onBindView:

Binds the 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?

like image 233
Koger Avatar asked Sep 08 '11 10:09

Koger


1 Answers

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.)

like image 85
Dan Lew Avatar answered Nov 15 '22 01:11

Dan Lew