Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 follow either of the approach:

  1. Call findViewById every time in Activity lifecycle.
  2. Get it first time, store the reference as a private instance variable of the Activity class.

Which approach is beter? What would be pros and cons of each approach in terms of performance and memory. Please help.

EDIT: If we move to new activity from A to B, we do not finish A as we want to open A on pressing back. In this scenario how to approach above problem? Please help.

like image 407
Abhishek Jain Avatar asked Jan 13 '14 09:01

Abhishek Jain


People also ask

Which activity lifecycle method would you use to clean up resources?

During the activity's lifecycle, the onStop() function is called. This means that some resources are released. The onStart() method can be invoked to initialize such resources.

Which lifecycle hook methods may not be called in low memory situations?

You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity. Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

When you get a call on your phone which life cycle method will be called?

The ondestroy() is the final call you receive before your activity is destroyed.

Which callback is called when the activity is no longer visible?

onStop() This callback is called when the activity is no longer visible. 6.


2 Answers

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.

like image 190
nKn Avatar answered Oct 23 '22 15:10

nKn


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.

like image 26
marcinj Avatar answered Oct 23 '22 16:10

marcinj