开发者

how to use variable while calling new activity in intent?

开发者 https://www.devze.com 2022-12-18 15:42 出处:网络
ihave following code to call new activity now i want to use variable to call new activity String var1,var2,var3;

i have following code to call new activity

now i want to use variable to call new activity

String var1,var2,var3; v开发者_StackOverflow中文版ar1="Login"; var2="Signup"; var3="more";

Intent i; 
i = new Intent(Favorites.this, Login.class);  --> login.class with var 
startActivity(i); 

can any one guide me how to achieve this???


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); 


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); 


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.


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();
}


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());
    }
}


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.


You can start an intent using a variable, like this

Intent intent = new Intent();
String packageName = "my.package"; 
String className = "com.package.MyActivity";
intent.setClassName(packageName, className);
startActivity(intent);
0

精彩评论

暂无评论...
验证码 换一张
取 消