onCreateView() being called too often in custom preference onCreateView() being called too often in custom preference android android

onCreateView() being called too often in custom preference


I don't know what have you implemented in this custom preference, but maybe the super class doesn't know how create a proper view to your preference?

From the documentation:

protected View onCreateView (ViewGroup parent)

Since: API Level 1 Creates the View to be shown for this Preference in the PreferenceActivity. The default behavior is to inflate the main layout of this Preference (see setLayoutResource(int). If changing this behavior, please specify a ViewGroup with ID widget_frame. Make sure to call through to the superclass's implementation.

http://developer.android.com/reference/android/preference/Preference.html

I guess you have set that id on the horizontal layout.

Now that I'm talking about it, why don't you include this horizontal layout in the layout that you are inflating?


I am not sure if the code you are using is an accurate test. I have a custom preference and I only override 5 methods and three of them are constructors.

public ImageButtonPreference(Context context){    this(context, null);}public ImageButtonPreference(Context context, AttributeSet attrs){    this(context, attrs, 0);}public ImageButtonPreference(Context context, AttributeSet attrs, int defStyle){    super(context, attrs, defStyle);    mInflater = LayoutInflater.from(context);    // This is where I pull all of the styleable info from the attrs    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageButtonPreference, defStyle, 0);    for(int i = a.getIndexCount(); i >= 0; i--)    {        int attr = a.getIndex(i);         switch (attr)        {        case R.styleable.ImageButtonPreference_logo:            mImageResource = a.getResourceId(attr, mImageResource);            break;        case R.styleable.ImageButtonPreference_button_text:            mButtonText = a.getString(attr);            break;        }    }}@Overrideprotected View onCreateView(ViewGroup parent){    View view = mInflater.inflate(R.layout.image_button_preference, parent, false);    return view;}@Overrideprotected void onBindView(View view){    super.onBindView(view);    ImageView image = (ImageView)view.findViewById(R.id.Logo);    if(image != null && mImageResource != 0) image.setImageResource(mImageResource);    Button button = (Button)view.findViewById(R.id.ConnectButton);    if(button != null)    {        button.setText(mButtonText);        button.setOnClickListener(mButtonListener);    }}

I have pulled this code, almost verbatim from the Android Source, so it should be just as fast as any other preference.


I was running into this problem and I tracked it down to the fact that I had the layout set both in the preferences.xml file and in my Preference subclass onCreateView() method. When I removed the layout from preferences.xml, onCreateView() stopped getting called multiple times.