Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent and start activity from string

I have a little problem. I want to start activity but in something other way. I know that

Intent i = new Intent(this, ActivityTwo.class); 

initialize intent and after that I can startActivity. But I want do something like that:

Intent i = new Intent(this, MyString.class);

I have no nameActivity.class, but I want change on string.class. How I can start activity when I have string name of class?

like image 475
user1302569 Avatar asked Feb 13 '13 09:02

user1302569


People also ask

How do I launch an activity using intent?

1.2. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

How do I start my activity results?

First you use startActivityForResult() with parameters in the first Activity and if you want to send data from the second Activity to first Activity then pass the value using Intent with the setResult() method and get that data inside the onActivityResult() method in the first Activity .


2 Answers

Try this: startActivity(this, Class.forName(yourStringClass));

like image 193
PaNaVTEC Avatar answered Nov 15 '22 08:11

PaNaVTEC


Here is a code by which you can start activity using the name of the activity

Class<?> c = null;
if(StringClassname != null) {
    try {
        c = Class.forName(StringClassname );
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Intent intent = new Intent(mail.this, c);
startActivity(intent);

Here class name will be full name of the class with the package name. For example if your package name will be x.y.z and if you have Activity name called A then the full name of the Activity A will be x.y.z.A.

like image 41
jlopez Avatar answered Nov 15 '22 08:11

jlopez