Restarting a task when starting an asynchronous task

I am confused with the async task. What do I need to do when my activity reloads? In my work, onCreate () starts the async task. I know that the activity is restarted when android is required (i.e. Orientation change or other). I have no problem with this ... and I think restarting a new async task is acceptable.

However, I have no idea what is going on with my previous async task. Did I destroy it?

My second question is, what if I have a progressDialog in my previous task. Should I reject this dialogue (and how)?

+3


source to share


2 answers


No, yours Asynctask

will end with your activity as well as yours progressDialog

. When your activity calls onRestart()

, it must first go through onPause

and onStop

, which will destroy your activity, but not your application.

More on Activities - http://developer.android.com/reference/android/app/Activity.html

Also, it would be safer to override yours Asynctask

and also set progressDialog

to null.



From the 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.)

+2


source


you need to save the activity instance and when you restart the activity you should resume your activity from that instance using this link: Saving some orientation change data in Android ya you need to skip this dialog.



0


source







All Articles