I am working on a remote Android code written by someone else, and it has several memory problems. One problem that I cannot understand is this. In the main Activity class, it has a static method called getReference (). This method basically returns a reference to a variable called mThis . Now, in the onCreate method
class MyActivity extends Activity
{
private static MyActivity mThis;
public MyActivity getReference(){
return mThis;
}
public onCreate(Bundle savedInstanceState){
mThis = this;
}
...
}
Now in a content provider or class expanding from the Dialog class, it uses this static method to get a context reference.
I am sure that this is not true and is one of the reasons for the memory leak. But is there a way to get the current context without actually passing the context object in each class. There is an application context, but I don’t think it has any method to get the current context.
I can remove this method, and then pass the context object reference to these classes. But is there any other way to fix this.
source
share