how to use variable while calling new activity in intent?

后端 未结 7 1529
失恋的感觉
失恋的感觉 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); 
    
    0 讨论(0)
  • 2021-01-22 01:48

    For

    String activity="com.namespace.MainActivity";
    

    This will do:

     private void openActivity(String activity, Context context) {
        try {
            Class cls = Class.forName(activity);
            Intent intent = new Intent(context, cls);
            context.startActivity(intent);
        } catch (ClassNotFoundException e) {
            Log.e(TAG,"Class was not found"+ e.getLocalizedMessage());
        }catch (Exception e){
            Log.e(TAG,"Error occurred"+ e.getLocalizedMessage());
        }
    }
    
    0 讨论(0)
  • 2021-01-22 01:52

    You cant pass a String in as the parameter it has to be an Activity.

    Use an if or switch statement to switch between the different selections you have.

    Something like this maybe....

    Intent i; 
    switch(var)
    case:Login
    i = new Intent(Favorites.this, Login.class); 
    break; 
    case:Signup
    i = new Intent(Favorites.this, Signup.class);
    break;   
    case:More
    i = new Intent(Favorites.this, More.class);
    break; 
    
    
    startActivity(i); 
    
    0 讨论(0)
  • 2021-01-22 01:54

    From the link Maurits gave, you can also have a look at the putExtras methods.

    See more info here and search for putExtra (the URL doesnt look good on SO)

    *Edited:

    From Donal Rafferty post, I think I understand now what you mean in the OP.

    What you can do is the below:

    String theClass = "Login";
    StringBuilder myAction = new StringBuilder("com.yourpackage.");
    myAction.append(theClass);
    Intent i = new Intent(myAction.toString());
    startActivity(i)
    

    The myAction string (in this example com.yourpackage.Login should be specified in the AndroidManifest.xml under an activity

    From the doc:

    public Intent (String action)

    Since: API Level 1 Create an intent with a given action. All other fields (data, type, class) are null. Note that the action must be in a namespace because Intents are used globally in the system -- for example the system VIEW action is android.intent.action.VIEW; an application's custom action would be something like com.google.app.myapp.CUSTOM_ACTION.

    0 讨论(0)
  • 2021-01-22 01:56

    If you want to add data, you'll have to use a different Intent constructor:

    public Intent(String  action, Uri  uri, Context  packageContext, Class<?>  cls)
    

    In the uri you can put your own information. See this link for more details.

    0 讨论(0)
  • 2021-01-22 01:59

    try this
    try {
      String className = 'com.www.tutorialforandroid.com.openActivity';
      Intent openNewIntent = new Intent( this, Class.forName( className ) );
      startActivity( openNewIntent );
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }

    0 讨论(0)
提交回复
热议问题