Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

android - What happens to an AsyncTask when the launching activity is stopped/destroyed while it is still running?

I've seen few questions nearly identical to mine, but I couldn't find a complete answer that satisfies all my doubts.. so here I am.. Suppose that you have an activity with an inner class that extends the AsyncTask class like this:

public class MyActivity extends Activity {            
    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
        protected Bitmap doInBackground(String... urls) {
            return DownloadImage(urls[0]); 
        }
        protected void onPostExecute(Bitmap result) {
            ImageView img = (ImageView) findViewById(R.id.img); 
            img.setImageBitmap(result);
        }  
    }

    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        new DownloadImageTask().execute("http://mysite.com/image.png")
    }
}

Suppose that the activity is paused or destroyed (maybe the two cases are different) while the DownloadImageTask is still running in background.. then, the DownloadImageTask's methods that run on the activity UI thread can be triggered and the DownloadImageTask may try to access Activity's methods (it is an inner class, so it can access the methods and instance variables of the outer class) with a paused or destroyed Activity, like the call to findViewByID in the example below.. what happens then? Does it silently fail? Does it produce any exception? Will the user be notified that something has gone wrong?

If we should take care that the launching thread (the Activity in this case) is still alive when running-on-UI methods are invoked, how can we accomplish that from within the AsyncTask?

I'm sorry if you find this as a duplicate question, but maybe this question is a bit more articulated and someone can answer with greater detail

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Consider this Task (where R.id.test refers to a valid view in my activity's layout):

public class LongTaskTest extends AsyncTask<Void, Void, Void>{
    private WeakReference<Activity> mActivity;
    public LongTaskTest(Activity a){
        mActivity = new WeakReference<Activity>(a);
    }
    @Override protected Void doInBackground(Void... params) {
        LogUtil.d("LongTaskTest.doInBackground()");
        SystemClock.sleep(5*60*1000);
        LogUtil.d("mActivity.get()==null " + (mActivity.get()==null));
        LogUtil.d("mActivity.get().findViewById(R.id.frame)==null " + (mActivity.get().findViewById(R.id.test)==null));
        return null;
    }
}

If I run this task from an Activity's onCreate like so:

public class Main extends Activity {
    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        setContentView(R.layout.testlayout);
        new LongTaskTest(this).execute();
        finish();
    }
}

No matter how long I sleep the background thread, my log always shows:

LongTaskTest.doInBackground()
mActivity.get()==null false
mActivity.get().findViewById(R.id.frame)==null false

Which is to say that the activity and its views appear to stay alive (even if I manually issue GCs via DDMS). If I had more time I'd look at a memory dump, but otherwise I don't really know why this is the case ... but in answer to your questions it appears that:

  • Does it silently fail? No
  • Does it produce any exception? No
  • Will the user be notified that something has gone wrong? No

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...