Is it good to call findViewById every time in Activity lifecycle whenever required?

前端 未结 6 1141
余生分开走
余生分开走 2020-12-20 13:56

Whenever we need a reference to the widget, we uses findByViewById.

When we are referring the widget lots of time in the code of the same Activity class, we can fol

6条回答
  •  天命终不由人
    2020-12-20 14:32

    Both approaches have their risks. In general, you should call findViewById() the less times you can, by the other hand, storing a reference on the Activity class may lead to memory leaks. It depends so much on what you want to do, how much times are you calling it and basing on it choose one of the approaches. For that, you'll need to analyze your code and if you're not clear about which is better, just try both and choose the "less bad", but generally the first approach is worse than the second one because you know you'll always have to find across ALL elements you've defined an id.

提交回复
热议问题