Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare the context objects of the two different Activities..?

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!

like image 205
Noby Avatar asked Mar 02 '12 13:03

Noby


People also ask

How do I find the context of an activity object?

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.

What is the difference between activity and context?

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.

What is the difference between activity context and application context?

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.


1 Answers

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
}
like image 92
ScouseChris Avatar answered Oct 15 '22 12:10

ScouseChris