Get Activity name dynamically - android
Asked Answered
S

6

89

I'd like to get the name of the current Activity to be sent along in the URI of an HttpRequest. Is there a way to do this without referring specifically to the Activity?

I know I can do myActivity.class.toString() but this is just a less efficient way of hard coding "myActivity" since I'm making a static reference to my Activity. Is there a more general way to do this using something like 'this' (which btw doesn't actually work here because it returns more information than what's desired).

Stillhunt answered 16/5, 2012 at 23:36 Comment(1)
I don't know that this works, but can't you just use this.getClass().getSimpleName() instead?Rodd
R
212

Use this.getClass().getSimpleName() to get the name of the Activity.

From the comments, if you're in the context of an OnClickListener (or other inner class), specify the class manually:

MainActivity.class.getSimpleName()

Rodd answered 16/5, 2012 at 23:40 Comment(4)
That's a good suggestion but not what I'm looking for. It works within onCreate(), but not from within my onClickListener which is where I would like to get the Activity name. If I can't no solution is better than using a constant string.Stillhunt
To do this within an onClickListener, you would do "MyActivityName.this.getClass().getSimpleName()"Interdenominational
You should note that if you are going to use ProGuard, your class name will get obfuscated and the obfuscated name is what you'll receive on the backend.Tetratomic
And you can use MainActivity::class.java.name in kotlin.Nasho
T
23

For purists out there who may not want to use reflection, an alternative way is to use the PackageManager as follows:

PackageManager packageManager = activity.getPackageManager();
try {
  ActivityInfo info = packageManager.getActivityInfo(activity.getComponentName(), 0);
  Log.e("app", "Activity name:" + info.name);
} catch (NameNotFoundException e) {
  e.printStackTrace();
}

However this seems like a lot of work just to do the same as getClass().getName() (and not even getSimpleName()). But I guess it may be useful for someone who wants more information about the activity than just the class name.

Tupler answered 10/9, 2013 at 9:49 Comment(0)
W
7
ActivityManager am = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
this.currentActivity = taskInfo.get(0).topActivity.getClassName();
Log.i( "CURRENT Activity ",  currentActivity);
Warily answered 13/10, 2016 at 6:53 Comment(2)
getRunningTasks is deprecated.Grandmotherly
This Code crashed Android 12, Android 13 devices.Click
N
5

First: open your manifest.xml file

you will find your package name i.e. "com.company.projectname"

Then: lets say your activity name is MainActivity

MainActivity.class.getCanonicalName() >output> "com.company.projectname.MainActivity"

OR

MainActivity.class.getSimpleName() >output> "MainActivity"

OR

MainActivity.class.getName() >output> "com.company.projectname.MainActivity"
Noxious answered 25/6, 2018 at 12:47 Comment(0)
G
1

For Xamarin

string GetActivityClassName(Activity activity) //Receiving LoginActivity
{
    //ComponentName
    activity.ComponentName; //Output: "md5101a0260d0a0e5d40a0a9009be09b0c2.LoginActivity"

    //LocalClassName
    activity.LocalClassName; //Output: "md5101a0260d0a0e5d40a0a9009be09b0c2.LoginActivity"

    //Java.Lang.Class.FromType.SimpleName
    Java.Lang.Class.FromType(activity.GetType()).SimpleName; //Output "LoginActivity"

    //Type.Name
    activity.GetType().Name; //Output "LoginActivity"
}
Ganges answered 20/3, 2019 at 8:7 Comment(0)
S
1

For anyone using Kotlin, try this:

this.javaClass.name
Sandrasandro answered 3/1 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.