When does an instance of Java – Activity die?

When does an instance of Java – Activity die? … here is a solution to the problem.

When does an instance of Java – Activity die?

Here’s a sample code that I miss a bit:

package com.leak;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
public class WindowLeakActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    new LeakThread().execute();
}


class LeakThread extends AsyncTask<Void, Void,Void>{

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog=new ProgressDialog(WindowLeakActivity.this);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(2000);
            finish();
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        //that would be ok
        if(WindowLeakActivity.this!=null && !WindowLeakActivity.this.isFinishing())
            dialog.dismiss();
    }


}}

As you can see, I created a LeakThread and completed the WindowLeakActivity in the doInBackground() method.

To prevent window leak errors, I have to check if the activity is done in the onPostExecute() method. This makes me miss it a little. I have the following questions:

  1. Is do Activity instance isFinish() check at onPostExecute safe? If my Thread class is not a inner class of Activity.Do I have to check Activity instance is not null at first?
  2. When would Activity instance die? As Activity’s lifecycle description,it will terminal when callback call onDestroy(). But however,the Activity’s Thread is still going. Though it’s window been not visible,I can also get it’s instance.
  3. If I call the System.gc(). Will it collect Activity’s instance?

Sorry for my misdescription. Thank you very much for reading my question.

Best Solution

1) In general, avoid using any references to activities in doInBackground().
Managing AsyncTask and the lifecycle of an activity is tricky at best. Check out this StackOverflow thread for a good discussion of AsyncTask and its pitfalls.

2) You have no control over when an activity instance ends, so don’t rely on it. The destruction of an activity instance depends on several factors, which are determined by the system. Therefore, try to make sure that you do not use a reference to an activity anywhere outside the scope of the activity object itself. However, you do receive callbacks when the execution of your activity is about to stop , so make sure to clean up memory there.

3) System.gc() is more like a request to the JVM to run the garbage collector as soon as it is convenient. Check out this thread .

From personal experience, I can tell you to avoid using ProgressDialog when using AsyncTask. It’s a pain to manage, it’s easy to leak your Window object, and once your device configuration changes, the application crashes and makes it almost impossible for you to debug it. I don’t even see Google apps on Android making perfect use of ProgressDialog (uptil Gingerbread ie). However, this is only my experience.

Related Problems and Solutions