-5

I have an class to start a new activity and I have a function inside that creates an intent. I need to pass MainActivity.class variable to that function. Normally I do it like this.

Intent intent = new Intent(context, MainActivity.class);

And I wan't to have something like this:

Intent intent = new Intent(context, variable);

So I wan't to change activity to start basically. Is this possible?

3 Answers3

0

Search for your question before you post one, a lot of people have the same question as you.

I guess this is what you're looking for

How to pass integer from one activity to another?

Now instead of int use string which i guess that what you're looking for

0

If I understood you correctly you want to use reflection to get the class name.

Class someActivity = Class.forName(getPackageName() + "MainActivity");
Intent intent = new Intent(context, someActivity);
startActivity(intent);
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • Exactly. If I edit this code like this I can get what I wanted. – Opsoftware Jul 13 '17 at 11:41
  • try { Class someActivity = Class.forName(getPackageName() + ".MainActivity"); Intent intent = new Intent(getApplicationContext(), someActivity); startActivity(intent); }catch (Exception e){ e.printStackTrace(); } – Opsoftware Jul 13 '17 at 11:41
0

You are looking for this one.

private void callActivity(Activity activity,Class<?> className)
{
    Intent intent = new Intent(activity, className);
    startActivity(intent);
}

Just pass activity from where you call another activity and the className which you need to call.

private void callActivity(Context context,Class<?> className)
{
    Intent intent = new Intent(context, className);
    startActivity(intent);
}
Chirag
  • 56,621
  • 29
  • 151
  • 198