how to use variable while calling new activity in intent?

后端 未结 7 1540
失恋的感觉
失恋的感觉 2021-01-22 01:08

i have following code to call new activity

now i want to use variable to call new activity

String var1,var2,var3; var1=\"Login\"; var2=\"Signup\"; var3=\"more

7条回答
  •  一向
    一向 (楼主)
    2021-01-22 01:46

    Edited: added variable activity class.

    You would set the variables on the intent as extras in the intent. You can easily pass the class name into your intent. So you could say:

    Class activityClass = Login.class;  // This could be passed in as a variable.
    
    Intent i; 
    i = new Intent(Favorites.this, activityClass);  --> login.class with var 
    i.putExtra("var1", "Login");
    i.putExtra("var2", "Signup"); 
    i.putExtra("var2", "more");
    startActivity(i); 
    

    Here is an example

    You can also put the variables in a bundle and pass the entire bundle as extras as follows:

    Class activityClass = Login.class;
    
    Intent i; 
    i = new Intent(Favorites.this, activityClass);  --> login.class with var 
    Bundle bundle = new Bundle();
    bundle.putString("var1", "Login");
    bundle.putString("var2", "Signup"); 
    bundle.putString("var2", "more");
    i.putExtras(bundle);
    startActivity(i); 
    

提交回复
热议问题