Every once in a while, entering in a new Activity requires updating values in let’s say several TextViews. So let’s say I got n Strings to write in n TextViews of the starting Activity.
Which one is the best approach to guarantee good performance and providing “clean code”?
Variant 1 (The way I actually apply):
I declare a single TextView variable “tempText” as global variable and assign the TextView to update to this variable (alternatively in a extra method).
Alternatively, a) is doing the whole procedere in the onCreate(), while b) is handle everything in a method called e.g. updateTextViews()
(...)
public class MyActivity extends Activity{
private TextView tempText;
public onCreate(Bundle icicle){
(...)
tempText = (TextView) findViewById(R.id.tv_1);
tempText.setText(string_1);
tempText = (TextView) findViewById(R.id.tv_2);
tempText.setText(string_2);
(...)
tempText = (TextView) findViewById(R.id.tv_n);
tempText.setText(string_n);
}
}
Variant 2 :
I declare a single TextView variable “tempText” as variable in the onCreate() or the respective method and assign the TextView to update to this variable.
The rest is in analogy to Variant 1.
(...)
public class MyActivity extends Activity{
public onCreate(Bundle icicle){
(...)
private TextView tempText;
tempText = (TextView) findViewById(R.id.tv_1);
tempText.setText(string_1);
tempText = (TextView) findViewById(R.id.tv_2);
tempText.setText(string_2);
(...)
tempText = (TextView) findViewById(R.id.tv_n);
tempText.setText(string_n);
}
}
Variant 3:
I declare a global TextView variable for every TextView to update. This, as far as I know, needs more space in the RAM, but I don’t know about the impact to velocity. Also here, are there differences between handling it in the onCreate() (a)) or in a seperate method (b))?
(...)
public class MyActivity extends Activity{
private TextView tempText_1;
private TextView tempText_2;
(...)
private TextView tempText_n;
public onCreate(Bundle icicle){
(...)
tempText_1 = (TextView) findViewById(R.id.tv_1);
tempText_1.setText(string_1);
tempText_2 = (TextView) findViewById(R.id.tv_2);
tempText_2.setText(string_2);
(...)
tempText_n = (TextView) findViewById(R.id.tv_n);
tempText_n.setText(string_n);
}
}
Variant 4:
I declare a TextView variable for every TextView to update in the onCreate() or the respective method which handles this. The rest is in analogy to Variant 3?
(...)
public class MyActivity extends Activity{
public onCreate(Bundle icicle){
(...)
private TextView tempText_1;
private TextView tempText_2;
(...)
private TextView tempText_n;
tempText_1 = (TextView) findViewById(R.id.tv_1);
tempText_1.setText(string_1);
tempText_2 = (TextView) findViewById(R.id.tv_2);
tempText_2.setText(string_2);
(...)
tempText_n = (TextView) findViewById(R.id.tv_n);
tempText_n.setText(string_n);
}
}
Which one is the “best” method? Variants 1 and 2 provide reserving only one memory address in the RAM and use this, while according to Robert C. Martins “Clean Code” the variables are really ambiguous. Option 3 and 4 would be the exact opposite. But for the rest I’m not very conscious of other effects.
Personally, I use a Variant 3 if I need to access the TextViews again elsewhere in Activity (i.e. in onResume, which is where I usually put my static UI updates). If the setText is a one-shot thing, I do something like —
If there’s any doubt that the view might not be there (i.e. when you’re modifying layouts during dev), I’d use a wrapper such as —
And then in onCreate:
safeSetText( R.id.tv_1, string_1 );
safeSetText( R.id.tv_2, string_2 );