Recycling attributes from destroyed activities?

498 Views Asked by At

I have a doubt about destruction of activities and objects.

While I attach & detach the activity from the AsyncTask I do not change the ArrayAdapter from the asynctask (see code). So, what I get is multiple activities being attached & detached (ought to orientation changes) and just one task running and modifying ONE adapter, which in turn is the one from the first activity that created the task. So, when I attach the task in the onCreate() I just set the adapter with the one which holds the task, which in turn has all the values processed (in the example just a dummie list of numbers).

How can this be possible? I thought that onDestroy() would erase the activity itself and its attributes, and therefore I would get a null pointer exception or something like that while trying to access the ArrayAdapter of the original activity from the AsynkTask, but the code below works!

private static class TestingTask extends AsyncTask<Void, Integer, Void> {

    private TestingActivity mActivity; // extends ListActivity
    private ArrayAdapter<String> mAdapter;
    private boolean mIsFinished;

    private TestingTask(Context activity) {
        attach(activity);
        mAdapter = (ArrayAdapter<String>)mActivity.getListAdapter();
        mIsFinished = false;
    }

    private void attach(Context activity) {
        mActivity = (TestingActivity)activity;
    }

    private void detach() {
        mActivity = null;
    }

    protected Void doInBackground(Void... params) {
        for (int i = 0; i < 100000; i++) {
            publishProgress(i);
        }
        return null;
    }

    protected void onProgressUpdate(Integer... values) {
        if (!isCancelled()) {
            mAdapter.add(values[0].toString());
        }
    }

    // ...
}

Is this because the task keeps an active reference to the ArrayAdapter object, and therefore it is not deleted? Or is it something else?

I also experienced another "similar case" in which I returned an Activity's attribute from onRetainNonConfigurationInstance() let's say A a, that had visibility over B b (which is another attribute of the Activity). Then, when trying to access b instance through a, there is no problem and I thought I would need a wrapper to hold the two instances (a and b), or else I would get an exception when trying to access b (which I do not actually save). I do not know if it is related width the previous case in which the objects that I supposed not to be available actually are there, maybe because of the active reference to them that causes no deletion?

Thank you!

1

There are 1 best solutions below

0
On BEST ANSWER

I think I have found the answer to these questions and as I was wondering... it is related to the Garbage Collector and the use of strong references.

In Understanding weak references article it is said that:

if an object is reachable via a chain of strong references (strongly reachable), it is not eligible for garbage collection. As you don't want the garbage collector destroying objects you're working on, this is normally exactly what you want

In another article How Gargabe Collection works it is explained that:

if an object holds reference of another object and when you set container object's reference null, child or contained object automatically becomes eligible for garbage collection.

So, my conclusion is that:

In the first case: As I am setting activity to null in detach() there is no memory leak and all objects can be garbage collected unless the adapter, which has a strong reference. So, I understand that the activity and all other objects contained by it are deleted unless the adapter, this is what I actually want.

In the second case: As I am returning the container object (A a) in onRetainNonConfigurationInstance() and it has a strong reference to (B b), b instance is accessible too, because it can be reachable via a chain of strong references.

Hope this will be helpful. If anyone else wants to give his/her opinion it will be welcome!