恰好在发射活动已停止/销毁,同时它仍在运行的AsyncTask的是什么? [英] What happens to an AsyncTask when the launching activity is stopped/destroyed while it is still running?

查看:117
本文介绍了恰好在发射活动已停止/销毁,同时它仍在运行的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")
    }
}

假设该活动被暂停或破坏(也许这两种情况是不同的),而 DownloadImageTask 仍在后台运行..然后,在 DownloadImageTask 的该活动UI线程上运行的方法可以被触发, DownloadImageTask 可尝试访问活动的方法(这是一个内部类,所以它可以访问外部类的方法和实例变量)与暂停或破坏活动,如调用 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?

如果我们要照顾的启动线程(在这种情况下,活动)仍然活着磨合上的UI方法被调用的时候,我们怎么能做到从内的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;
    }
}

如果我运行从活动的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手动发出GCS)。如果我有更多的时间,我想看看内存转储,但除此之外,我真的不知道的为什么的是这样的话......但在回答你的问题似乎是:

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天全站免登陆