jondavidjohn
jondavidjohn

Reputation: 62392

Android Efficiency - Views

Is it better to findViewById(<resource-id>) once and store in the scope of your activity (class-wide) or call the above per use, so that the resources are reclaimed when the current scope/method is exited?

I guess it boils down to how expensive findViewById() is compared to how expensive it is to store view objects class-wide.

Upvotes: 4

Views: 587

Answers (2)

maxpower47
maxpower47

Reputation: 1656

You're only storing references to views; the storage cost is minimal. Most quality code I've seen finds the view once for each activity, so I take that as best practice. Readability and maintainability are just bonuses.

Upvotes: 5

Cristian
Cristian

Reputation: 200090

I think the performance difference is imperceptible. More important than that is how your code looks like... is it readable when you populate your activity class with findViewById invocations?

Upvotes: 0

Related Questions