AsyncTask neasted or own class?

2019-09-05 16:47发布

is it in general better to create AsyncTask as private class within an Activitiy, or rather separate them in an own class?

public class MyActivity extends Activity {
  private class DownloadPage extends AsyncTask {
  }
}

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-05 17:41

AsyncTask inside Activity is more suitable after completing backgroud task you can change view easily.

In seperate class you need to pass context and its result comes in seperate class and then you have to get result from seperate class.

But seperate class is very useful sometimes when we need more asyncTask in application , and if you have large code in your activity so its better to use seperate class.

So AsyncTask is suitable inside Activity to interact with view...and seperate class is also suitable when we need more asynctask in application.

So its totally depends on requirements...

查看更多
放荡不羁爱自由
3楼-- · 2019-09-05 17:49

A private class within the Activity has the access to all private members, including context, which greatly helps not only to do the job, but to show ProgressDialog as well.

查看更多
登录 后发表回答