4

I'm confused with the async task. What do I have to do when my activity restarts? In my activity, onCreate() starts an async task. I know that the activity restarts when android requires it (i.e. orientation change or other). I've no problem with this... and I think restarting a new async task is acceptable.

Nevertheless I don't know what's happening with my previous async task. Do I have destroy it ?

My second question: what if I have a progressDialog in my previous task. Do I have to dismiss this dialog (and how)?

user2052680
  • 131
  • 1
  • 6
  • http://logc.at/2011/11/08/the-hidden-pitfalls-of-asynctask/ – baboo Feb 13 '13 at 10:06
  • Your first first question "What do I have to do when my activity restarts?" You should call YourAsyncTask.execute() inside onResume() method. If you change orientation I think that better solution is store your data and not destroy your AsyncTask and execute it again. To cancel your progressDialog just call YourAsyncTask.cancel(true); – Martin Feb 13 '13 at 10:09
  • Progress Dialogs s*cks ... and you should avoid em ... http://developer.android.com/guide/topics/ui/dialogs.html (search for "Avoid ProgressDialog") – Selvin Feb 13 '13 at 10:37

2 Answers2

2

No, your Asynctask will end with your activity as well as your progressDialog. When your activity calls onRestart() it must first go through onPause and onStop which will destroy your activity but not your application.

For more information on activities - http://developer.android.com/reference/android/app/Activity.html

Additionally, it would be safer to cancel your Asynctask as well as set progressDialog to null.

From Asynctask documentation

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

Synaero
  • 479
  • 5
  • 12
0

you have to save the instance of activity and when your activity restart you have to resume your activity with that instance use this link: Saving some data on orientation change in Android ya you have to dissmis that dialog.

Community
  • 1
  • 1