So usually in guides when making a holder for a View let’s say Button, I usually see it on the onCreate method like this:
public class className extends Activiy{
public void onCreate(){
final Button button = (Button) findViewById(r.something.something);
}
}
Well I was wondering if there’s any difference and if ever there is, which is a better in approach from that one to this one:
public class className extends Activiy{
Button button;
public void onCreate(){
button= (Button) findViewById();
}
}
The first creates a method local variable, the second creates a field in each instance of your Activity class.
It is always better to use the smalles (that is most local) scope possible. On Android the memory usage makes this even more important. Therefore please go with the first solution (assuming that you do not need to reference that button from multiple other places in your code).