Static Link Activity

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.

+3
source share
2 answers

I am sure that this is not true and is one of the reasons for the memory leak.

Yes.

But is there a way to get the current context without actually passing the context object in each class.

There is no current context. Please put in Contextfor any method that he needs. For example, you will notice that most of the Android SDK is configured this way.

+8

ContentResolver Dialog , . mThis ?

0

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


All Articles