Java – is this an effective way to hold static references and activity / context? Why don’t I do that?

I have an abstract basefragmentactivity class. All activities in my Android application are extended. In this class, I maintain the static reference of the currently running activity as scurrectactivity. In my onstart() lifecycle callback, I set it as follows:

public abstract class BaseFragmentActivity extends FragmentActivity { 

    private static BaseFragmentActivity sCurrentActivity;

    public static BaseFragmentActivity getCurrentActivity(){
        return sCurrentActivity;
    }

    @Override
    protected void onStart() {
        super.onStart();
        sCurrentActivity = this;
    }

Using it, I can get the current activity / context from anywhere in the code by calling:

BaseFragmentActivity.getCurrentActivity()

So everything I've read says that I shouldn't hold static references to activities / contexts. But if this static variable is shared among all activities, do I really leak a context every time I start a new activity? I've read Romain guy's post on avoiding memory leaks( http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html ), watched the leak view on the Android Developer channel( https://youtu.be/h7qHsk1nWKI and https://youtu.be/BkbHeFHn8JY )Performance mode video, I have reason to believe that this is a bad practice. But someone can help me understand the exact cause of the leak and the location, if any. I want to put forward a case to eliminate this situation, but I need a stronger argument than "I think it is leaking a background"

Edit: for the sake of clarity, this is my inherited code base, in which all contents are very tightly coupled and chaotic. This function can find the context object almost anywhere. The internal dialog button callback to trigger the new idea inside the fragment, and even (for unknown reasons) get the view inside the fragment to get the view inside the parent activity! I need to provide our product owners with refactoring efforts on new features in a way they understand

resolvent:

You can disclose at most one activity instance in a given time. If you add

 @Override
    protected void onStop() {
        super.onStop();
        sCurrentActivity = null;
    }

Then you won't leak anything

But I really don't understand why you need to do this. What do you want to achieve?

Editor: after you explain what you want to achieve, it seems more reasonable to extend the application and keep the static reference and access it from anywhere. I think there are three better reasons:

>As long as your application is running, the application instance will remain active. Sometimes even when the system is not running, the system will not release it so that it can be ready for the next startup of your application. > I bet the activity object has more memory overhead than the application object. > when your application object is killed, I'm 99% sure your process also exists, so static references won't lead to leakage

Some code examples:

public class App extends Application {
    public static App context;
    public void onCreate() {
       super.onCreate();
       context = this;
    }
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>