I have a class where I am getting context objects from more than 10 activities.
I want to know the context object of which activity is at the instant.
I have tried the following but no results.
context.equals(One.this);
context.equeals(One.class);
If any one having any idea please share with me!
From B activity you create a object of class A using this constructor and passing getApplicationContext(). Show activity on this post. If you need the context of A in B, you need to pass it to B, and you can do that by passing the Activity A as parameter as others suggested.
In android, Context is the main important concept and the wrong usage of it leads to memory leakage. Activity refers to an individual screen and Application refers to the whole app and both extend the Context class.
Application Context: It is the application and we are present in Application. For example - MyApplication(which extends Application class). It is an instance of MyApplication only. Activity Context: It is the activity and we are present in Activity.
I hope you aren't holding on to these Context
references longer than necessary, I found out what a wonderful source of memory leaks this can be if not handled correctly!
If they are all Activity
instances you can treat them as such and use:
if ( activity instanceof MyClassActivityOne ) {
// do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With