Intention and start of activity from the line

I have a little problem. I want to get started, but in a different way. I know that

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

initialize the intention, after which I can startActivity. But I want to do something like this:

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

I haven't nameActivity.class, but I need to change to string.class. How can I get started when I have a class string name?

+5
source share
5 answers

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

+6
source

Here is the code with which you can start your activity using the activity name

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 the class name will be the fully qualified class name with the package name. For example, if your package name is xyz, and if you have an action name A, then the full name of Activity A will be xyzA

+6

You can search Classby name usingClass.forName("MyString")

+2
source


Class<?> c =Class.forName("YOUR STRING" );
Intent intent = new Intent(FirstActivity.this, c);
startActivity(intent);
+1
source

Just use ....

  Intent intent = new Intent().setClassName(activity,"packageName"+"className");
  startActivity(intent);
+1
source

All Articles