Is it possible in Android to save in a variable a reference to an Activity to be used later in an Intent?

1

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 a Activity (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?

    
asked by A. Cedano 06.12.2018 в 20:51
source

2 answers

0

From my point of view I do NOT recommend that you keep references of your activitys

What I suggest and I can think of at this moment is this:

public class Test extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        onItemClick(1);//pasa el item
    }

    public void onItemClick(int item) {
        switch (item) {
            case 1:
                starActivity(EditImageActivity.class);
                break;

            case 2:
                starActivity(MisaActivity.class);
                break;

            case 3:
                starActivity(HomiliasActivity.class);
                break;
        }
    }

    private void starActivity(Class<?> activity) {
        Intent i = new Intent(this, activity);
        startActivity(i);
        finish();

    }

}
    
answered by 07.12.2018 в 19:49
-1

There are good and bad practices about how to write code, I would not know where your question fits, but if you want to save code lines, I recommend this: To save your activity.class in a variable you should assign it to a variable in each case, I am adding 2 lines of code, since first you must create the variable before the switch and in each case assign a value to it later at the end of the switch start the activity, this in a final are 4 lines of code, which you can summarize to 1 for each case in the following way

        startActivity(new Intent(this, ComentariosActivity.class));

I hope you understand, anything answers my question!

    
answered by 06.12.2018 в 21:18