I have the following code, which listens for the clicks that occurred in the elements of a RecyclerView
to call another Activity
according to the item pressed.
public void onItemClick(MainItem item) {
Intent i;
switch (item.getItemId()) {
case 1:
i = new Intent(getApplicationContext(), BreviarioActivity.class);
startActivity(i);
break;
case 2:
i = new Intent(this, MisaActivity.class);
startActivity(i);
break;
case 3:
i = new Intent(this, HomiliasActivity.class);
startActivity(i);
break;
case 4:
i = new Intent(this, SantosActivity.class);
startActivity(i);
break;
case 5:
i = new Intent(this, LecturasActivity.class);
startActivity(i);
break;
case 6:
i = new Intent(this, ComentariosActivity.class);
startActivity(i);
break;
case 7:
i = new Intent(this, CalendarioActivity.class);
startActivity(i);
break;
case 8:
i = new Intent(this, OracionesActivity.class);
startActivity(i);
break;
case 9:
i = new Intent(this, MainMasActivity.class);
startActivity(i);
break;
default:
}
}
As it is the code works well, but I think that in each case
could save me these two lines:
i = new Intent(this, ...);
startActivity(i);
But for this you should save a reference to each Activity
according to the case
, and then do, outside the case something like this:
Intent i = new Intent (this, refActivityGuardada);
startActivity(i);
refActivityGuardada
would equal each reference to the different Activity
according to each case
.
What I do not know is how to save this variable: BreviarioActivity.class, MisaActivity.class
, etc.
Is it possible to save it? How?
Is it convenient to do it?
I have seen answers that recommend using
Reflection
for this. My doubt is whether it is possible to save aActivity
(which is not another thing that a class) how any variable is saved and if It would be worth doing to save some lines of code.Or, put another way: is it possible to save a class as it is save a string, an integer, a boolean? What would be the price of do what? worth it? I mean, I do not want to resort to procedures dark for saving a few lines of code.
Or, is not it possible to do it? Why is it not possible?