MikkoP
MikkoP

Reputation: 5092

Getting launcher activity class name

Is there a way to get the class name of the activity that launched the Intent without putting extras to the Intent?

I use the following code to launch other activities.

Intent intent = new Intent(CallerClass.this, TargetClass.class);
startActivity(intent);

Can I get the caller class' name in the target class?

Upvotes: 3

Views: 1853

Answers (3)

madx
madx

Reputation: 7203

String packageName = context.getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
String className = launchIntent.getComponent().getClassName();

Upvotes: 1

Elaine McGovern
Elaine McGovern

Reputation: 269

One way you could do it is by using startActivityForResult() instead of startActivity(Intent) and then have the second activity use getCallingActivity() to get the CallerClass.

Upvotes: 2

OWZY
OWZY

Reputation: 531

use startActivityForResult then you can retrieve activity caller by : getCallingActivity().getClassName()

Upvotes: 1

Related Questions