I am working with this following switch case to handle different buttons, and launch an activity in each case.
I am wondering– should I move the Intent object (i) outside– maybe as a private class attribute and re-use it? Or, should I wrap each case in brackets so I can re-use the name “i” without appending a unique name to it? Which is safer/better? Is there a risk in re-using the same object?
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_numbers:
Intent i = new Intent(v.getContext(), DrillActivity.class);
i.putExtra("word_list", R.raw.wl_numbers);
startActivity(i);
break;
case R.id.button_colors:
Intent i2 = new Intent(v.getContext(), DrillActivity.class);
i2.putExtra("word_list", R.raw.wl_colors);
startActivity(i2);
break;
case R.id.button_daysmonths:
Intent i3 = new Intent(v.getContext(), DrillActivity.class);
i3.putExtra("word_list", R.raw.wl_daysmonths);
startActivity(i3);
break;
default:
break;
}
}
I would do it this way:
I think it is much clearer.