当启动活动仍在运行时停止/销毁时 AsyncTask 会发生什么? [英] What happens to an AsyncTask when the launching activity is stopped/destroyed while it is still running?

查看:31
本文介绍了当启动活动仍在运行时停止/销毁时 AsyncTask 会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过几个与我几乎完全相同的问题,但我找不到可以满足我所有疑问的完整答案..所以我在这里..假设您有一个内部类的活动,该活动扩展了 AsyncTask 类如下:

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")
    }
}

假设 Activity 暂停或销毁(可能两种情况不同)而 DownloadImageTask 仍在后台运行.. 那么,DownloadImageTask 的方法可以触发在 Activity UI 线程上运行的 DownloadImageTask 可能会尝试访问 Activity 的方法(它是一个内部类,因此它可以访问外部类的方法和实例变量)并暂停或销毁 Activity,就像下面示例中对 findViewByID 的调用一样......然后会发生什么?它会默默地失败吗?它会产生任何异常吗?是否会通知用户出现问题?

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?

如果我们应该注意在调用 running-on-UI 方法时启动线程(在本例中为 Activity)仍然处于活动状态,我们如何从 AsyncTask 内完成该任务?

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

推荐答案

考虑这个任务(其中 R.id.test 指的是我活动布局中的有效视图):

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;
    }
}

如果我像这样从 Activity 的 onCreate 运行此任务:

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

也就是说,活动及其视图似乎保持活动状态(即使我通过 DDMS 手动发出 GC).如果我有更多时间,我会查看内存转储,否则我真的不知道为什么会出现这种情况……但在回答您的问题时,似乎:

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:

  • 它会默默地失败吗?没有
  • 它会产生任何异常吗?否
  • 是否会通知用户出现问题?否

这篇关于当启动活动仍在运行时停止/销毁时 AsyncTask 会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆