Can I send class as extra with intent?

Jesus Dimrix picture Jesus Dimrix · Mar 10, 2013 · Viewed 10.7k times · Source

I'm trying to pass class name with extra, how to do that ?

Intent p = new Intent(StartScreen.this, Setting.class);
p.putExtra(" ",StartScreen.this);

I want to get the class name in Setting class but I don't want it to be String cause I'm going to use this class name like that :

Bundle extras = getIntent().getExtras();
extras.getString("class");
Intent i = new Intent(Setting.this, class);
startActivity(i);

Answer

user1525382 picture user1525382 · Mar 10, 2013

you can use this code

Intent p = new Intent(StartScreen.this, Setting.class);
p.putExtra("class","packagename.classname");

and in setting class

Bundle extras = getIntent().getExtras();
String classname=extras.getString("class");
Class<?> clazz = Class.forName(classname);
Intent i = new Intent(Setting.this, clazz);
startActivity(i);