views:

514

answers:

4

dear friends,

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

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

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

A: 

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.

Maurits Rijk
+1  A: 

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.

ccheneson
I assume myclass.append(theClass) should be myAction.append(theClass)
Maurits Rijk
@Maurits - oops, i just fixed it - thanks
ccheneson
+5  A: 

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); 
Jay Askren
no no your are giving me example of passing value to another activity but i want to call activity dynamically i want to replace login.class with variable.. thats the questions..
UMMA
So you want to replace Login.class with a variable then. What exactly is stopping you from doing that?
Matthias
Login.class can be a variable as Matthias said. See the updated answer.
Jay Askren
+2  A: 

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); 
Donal Rafferty
just that you can't switch `String`s in Java ;-)
Matthias
True but ya have to let people figure out some things for themselves :-)
Donal Rafferty