Search code examples
androidandroid-activityintrospection

Get Activity name dynamically - android


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).


Solution

  • 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()