Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android get current activity's name for polymorphic method

I have a dilemma, basically I have ImageButtons that I want to be displayed on all activities (this is done) and I also want them to be displayed on all activities a certain way, depending on which activity is being displayed. I want this to be done in a way that I don't have to hardcode the specifications individually in each class.

Although the images are defined in one shared XML document, I want a shared class to be able to read which activity is being called, and then format a particular image a certain way. This is so if I change the functionality, I only have to change attributes in one class.

I want to do some sort of If or switch statement that compares if( thisActivity == MainActivity ) then, but I don't know how to retrieve the "names" of the activities to compare them!

edit. I want something like this:

   Context myContext = this;

    if(myContext instanceof Intent(MainMenu.class) == true)
    {

    }

I know the Intent function doesn't have a prototype to check a class like that, but I want something that will let me know the current class is part of a particular class, as opposed to several other cases in the conditional statement

thank you for any insight!

like image 814
CQM Avatar asked Apr 14 '11 20:04

CQM


People also ask

How can I get current activity name?

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

What is Android Activity name?

An activity represents a single screen with a user interface just like window or frame of Java. Android activity is the subclass of ContextThemeWrapper class. The Activity class defines the following call backs i.e. events. You don't need to implement all the callbacks methods.


1 Answers

Will the instanceof operator suit your needs?

if(this instanceof Activity1) {
//...
}
else if(this instanceof Activity2) {
//...
}
like image 144
LeffelMania Avatar answered Oct 04 '22 04:10

LeffelMania