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

前端 未结 6 1137
余生分开走
余生分开走 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

    Most developers use method 2, mostly because its more effective. If your layout is complicated then findViewById must traverse its tree to find given widget which takes time. In list views you mostly use ViewHolder pattern which allows you to store references to list item widgets. Since lists are redrawn very ofthen this greatly speeds up its rendering.

    Storing widgets in private references is quite safe, those references gets invalidated on configuration changes, but your activity is also destroyed then.

提交回复
热议问题