Java – weak reference instead of getactivity () (Android avoids memory leakage)?

In order to avoid memory leakage, I wrote the following method, which will be used for activities, mainly for fragments (using inheritance) This method should allow me to never reference the activity directly through a call

//this or getActivity()

The method is:

private WeakReference<BaseActivity> activityWeakReference = null; 

public BaseActivity getActivityFromWeakReference(){
        activityWeakReference = activityWeakReference == null ?
                new WeakReference<BaseActivity>((BaseActivity)getActivity()) :
                activityWeakReference;
        return activityWeakReference.get();
    }

According to the memory leak threat, is it safe to call this method getactivityfromweakreference() instead of getactivity()?

If this is not safe, should I return activityweakreference and call its get () method to make it safe?

I've been using multiple clips and I haven't had any problems so far I asked this question because I read this (here):

So far, I haven't encountered a situation where the mentioned elements exceed the activities If you find errors or possible errors, please write them down in the comments

Solution

This is entirely feasible For example, you have this pseudo code:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         new DownloadTask().execute();
    }

    public void showInfo() {
    }

    class DownloadTask extends AsyncTask<Void,Void,Void> {
        @Override
        protected Void doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void data) {
            // we can call showInfo() activity because Asynctask hold an implicit reference to activity 
            showInfo();
        }
    }
}

In the above code, there is a situation that will lead to memory leakage

This is an explanation:

When you create a downloadtask, as shown in the above example, Java calls downloadtask as an inner class The inner class implicitly holds a reference to the outer class, in this case mainactivity Moreover, when you start asynctask, asynctask will be saved by the system until it is completed For example, your download takes 30 seconds You can rotate the device within 30 seconds When rotating a device, the mainactivity is re created and usually destroys the old activity However, in this case, the old activity will not be destroyed because the old mainactivity instance is saved by downloadtask, which is retained by the system You are about to leak an active instance

To fix this problem, you should change the above code to:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new DownloadTask(this).execute();
    }

    public void showInfo() {
    }
}

class DownloadTask extends AsyncTask<Void,Void> {
    WeakReference<MainActivity> mainActivityWeakReference;

    public DownloadTask(MainActivity activity) {
        mainActivityWeakReference = new WeakReference<MainActivity>(activity);
    }

    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void data) {
        if (mainActivityWeakReference.get() != null) {
            mainActivityWeakReference.get().showInfo();
        }
    }
}

In this case, when a new mainactivity is created, the old mainactivity will not be retained by the downloadtask (due to the weak reference property), so the old one will be destroyed by the Android garbage collector in the future You should also check every time you use weakly referenced objects, because you don't know exactly when GC will destroy them

This is another case of memory leakage in my own blog Memory leak when using static inner class

I hope it helps

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
分享
二维码
< <上一篇
下一篇>>