1

Is it possible to get a class name based a string. For example

String activityName = "Activity";
Intent i  = new Intent(context, ...);

Some how convert the string to call the activity class like

Intent i = new Intent(context, Activity.class);
safaiyeh
  • 1,655
  • 3
  • 16
  • 35

2 Answers2

1

This is very possible. First, you will have to use the full name of the activity instead of the the file name, such as com.package.Activity.

String className = "com.package.Activity";

Then you can create a Class using that name like so:

Class<?> myClass = Class.forName(className);

Simply create a new instance of this Class and cast it to an Activity.

Activity myActivity = (Activity) myClass.newInstance();
Evan Ogra
  • 206
  • 3
  • 11
0

Try like this

String activityName ="com.project.net.Activity";// provide the class name with domain 
Intent i= new Intent( context, Class.forName(activityName));
startActivity(i);
safaiyeh
  • 1,655
  • 3
  • 16
  • 35
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80