Is this more performant than findByViewId?

50 views Asked by At

Let's say I have a custom view:

<com.xx.xx.xx.CustomView
     android:id="@+id/customView"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:onClick="doAction"/>

Then in my Activity I have:

public void doAction(View v) {
    if(customView == null) customView = (CustomView) v;
    ... do stuff
}

So as you can see, first time I click the view, I retrieve it and store it in an Activity field. Then I can use it anywhere. With this, I don't need to use findViewById.

I have to questions:

  1. Is this more performant or is the same?
  2. Is this a correct way to go?

Many thanks!

0

There are 0 answers