0

I have a ListActivity that instantiates an AsyncTask, makes a call to a web service, and populates the ListView with the results.

How should I handle device rotation while the AsyncTask is still running? Should I cancel it, save off whatever data I need, and start a new one when the ListActivity is recreated? Does Android somehow already handle such a case?

Andrew
  • 20,756
  • 32
  • 99
  • 177

3 Answers3

0

It seems like what you require here is a service, not an Asynchtask - as you are running a long query that should persist and deliver it's results the same way regardless of orientation.

BTW, killing the Asynctask is NOT straightforward - best not to go there at all, but let a service run truly in the background.

Yossi
  • 1,226
  • 1
  • 16
  • 31
  • I've tried this before and it has 1 major flaw. I used a BroadcastReceiver to notify the ListActivity that the query was finished and to hand the data to the ListActivity. However, the BroadcastReceiver is unregistered during onPause(), which means if the user launches another app or my app is interrupted during the query, my ListActivity will not "hear" the message telling it that the query is finished. – Andrew Jul 18 '11 at 19:44
  • Take a look at http://stackoverflow.com/questions/1538796/update-android-listactivity-when-list-data-changes - you may want to explore that. – Yossi Jul 18 '11 at 20:03
  • @Andrew: Have you figured out the solution for the same ?, I am too facing this issue. Trying to find out the solution. – Nandagopal T Oct 27 '12 at 06:51
  • Hey, Nandagopal T. Look at the answer I have marked as the correct answer. There is a link to another question. The answer of this question solves the problem. – Andrew Oct 29 '12 at 14:25
0

Do you require that your ListActivity is recreated on orientation change? I would expect that your users would not want that - but would rather have the populating of the ListView carry on.

I have an app that does a series of HTTP GETs and POSTs within a string of AsyncTasks, without restarting the activity each time the orientation changes. All you need is a line similar to the following in your manifest.

android:configChanges="orientation"

See the docs at http://developer.android.com/guide/topics/manifest/activity-element.html

Torid
  • 4,176
  • 1
  • 28
  • 29
  • Isn't this frowned upon? I seem to remember reading that Android handles a lot of things during orientation and if you override them, you're opening yourself up to problems. – Andrew Jul 18 '11 at 19:58
  • Well the docs do say "Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change." But in practice, it works just fine, and I'm pretty sure a lot of apps rely on it. And any other approach will have the effect of restarting your web request on orientation change - which is hardly what users expect? – Torid Jul 18 '11 at 20:07
  • I've implemented this and it seems to be working great (on emulator). I'll have to do some extensive device testing before I'm comfortable releasing it. Thank you. – Andrew Jul 18 '11 at 20:08
  • And looking at http://developer.android.com/guide/topics/resources/runtime-changes.html they are much more reasonable, saying "However, you might encounter a situation in which restarting your application and restoring significant amounts of data can be costly and create a poor user experience" where they present this as a option. – Torid Jul 18 '11 at 20:09
0

Torid's suggestion of overridding the config changed handler works in most situations. However, I've found that some manufacturer's devices still recreate the activity, even when you've done this (I've seen 1 HTC phone that does it so far).

The proper solution is CommonsWare's answer in the following link:

Background task, progress dialog, orientation change - is there any 100% working solution?

Community
  • 1
  • 1
Andrew
  • 20,756
  • 32
  • 99
  • 177