What to do with AsyncTask in onPause()?

后端 未结 4 1996
无人共我
无人共我 2020-12-09 13:07

I\'m using an AsyncTask in my activity. Looks like:

public class MyActivity {
    private AsyncTask mTask;

    private void doSomethingCool() {
        mTas         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 13:39

    Before diving into the fun that is AsyncTask and orientation changes, let me first ask this - do you need to hold onto the AsyncTask in a variable? There are not many reasons that you need to do this, because AsyncTask has callbacks built-in, so you don't actually need to hold onto your AsyncTask (don't call him, he'll call you). Also, your AsyncTask should continue running even during an orientation change - it's in another thread, after all.

    The only time I've had to hold onto AsyncTask was to fix a bug that occurs when you try to show a Dialog while the AsyncTask is running. There's a fix, but it's pretty nasty. There's a lot of discussion of that here, if that's the specific problem you're running into.

提交回复
热议问题