-2

My Application is running for Android 2.3.3 to 4.2.2

I want to avoid the restart of the Activity by rotating the display. Also I added the attribute android:configChanges="orientation|screenSize" in the manifest to these Activity. Thanks LogCat I could see that onConfigurationChanged() is called instead onCreate() while rotating the device. But only in higher Versions of Android! in 2.3.3 still onCreate() is called after onConfigurationChanged()

I want to avoid my Application for restarting, because I download some data in an AsyncTask. I think that's the simplest way to make sure, that the AsyncTask isn't call for any times and to make sure, that it isn't canceled.

Do you think it's a good idea, to avoid the Activity for restarts in my case when the configuration is changed? How could I handle this for Android 2.3.3?

TN888
  • 7,659
  • 9
  • 48
  • 84
lis
  • 670
  • 1
  • 13
  • 32
  • 1
    It's like trying to stop a river with a dam. Sure it works, but you'll destroy the environment. Adapt your application to the environment (android OS) and don't try to change the environment to your needs, because it'll get bloody. – STT LCU Jul 26 '13 at 08:28
  • yes, I agree with you! I haven't any experience for this case and didn't know how to handle it otherwise, it was just the simplest way for me. But that's why I asked the question ;) – lis Jul 26 '13 at 08:44

1 Answers1

3

Do you think it's a good idea, to avoid the Activity for restarts

Not really. Actually, using the android:configChanges attribute to handle orientation changes is not recommended, and this clearly is specified in android docs: "using this attribute should be avoided and used only as a last-resort."
One reason why this is not adviced is because there are other configuration changes (besides screen orientation) which could produce the re-creation of activity, and there’s a good chance that we won’t handle them all.

However, there are few possible solutions to handle properly this particular situation.

  1. You could use an IntentService instead of an AsyncTask. A service runs in background and is decoupled from the activity life cycle, so you won't be affected by screen orientation change.

  2. Put the AsyncTask in a Fragment. Fragments have the ability to retain their instances.

  3. Lock programmatically the screen orientation while the task is executing. The simplest, but not very adviced as this will break the user experience.

Take a look over this blog post for examples:

Andy Res
  • 15,963
  • 5
  • 60
  • 96
  • 2
    Well said. Also, Loaders. – josephus Jul 26 '13 at 08:40
  • Seems to me, that the first way is the best. I've got more than one AsyncTask, e.g. one show the user a ProgressDialog while executing, also I couldn't (or don't have to) replace it by an `IntentService`, right? But one download data in backround, another clean database in an interval. I think it's the best way to put these in `IntentService`? Is it ok to run more than one `AsyncTask` and more than one `IntentService` on the "same time" (start an other during one is processing). – lis Jul 26 '13 at 09:54
  • And maybe you can say me, what is happened with them, when Activity is restarted? When it is shut down, is `onCanceled` returning true in `AsyncTask`? – lis Jul 26 '13 at 09:56
  • When the activity is restarted the AsyncTask will still continue to do its work. But when it will finish, it will try to deliver the result to the old activity (the one destroyed), and eventually the app will crash. Yes, download the data in an `IntentService`. Basically, you should move the code from `doInBackground()`, in `onHandleIntent()` method of IntentService. And it's ok to run more than one `IntentService`. The tasks will be put in a queue and executed one at a time (not parallel). – Andy Res Jul 28 '13 at 17:01
  • I tried it! But I've to use a `static` member, which is definitely manipulated before calling the `IntentService`. But it seems so, that it isn't manipulated when calling the Intent (in `onHandleIntent` it has the original value, not the manipulated one). – lis Jul 29 '13 at 06:02
  • I just add the `static` member to the `Intent` via `putExtra` instead calling it directly and it worked fine! Tested it during rotating the device. Thank you! Now I transfer all `AsyncTasks` which don't give feedback to the user to `IntentServices` – lis Jul 29 '13 at 07:19