Android gets current activity name for polymorphic method

I have a dilemma, basically I have ImageButtons that I want to display in all actions (this is done), and I also want them to be displayed in all actions in a certain way, depending on what activity is displayed. I want this to be done in such a way that I don’t need to hardcode the specifications individually in each class.

Despite the fact that images are defined in one common XML document, I want the shared class to be able to read which action is invoked and then format the specific image in a specific way. This is so, if I change the functionality, I only need to change the attributes in one class.

I want to make some kind of If or switch statement that compares if (thisActivity == MainActivity), but I don't know how to get the "names" of actions to compare them!

to change. I want something like this:

Context myContext = this; if(myContext instanceof Intent(MainMenu.class) == true) { } 

I know that the Intent function does not have a prototype to test such a class, but I need something to let me know that the current class is part of a specific class, unlike several other cases in the conditional expression

Thank you for understanding!

+4
source share
1 answer

Does the instanceof operator suit your needs?

 if(this instanceof Activity1) { //... } else if(this instanceof Activity2) { //... } 
+18
source

Source: https://habr.com/ru/post/1348174/


All Articles