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.
You could use an
IntentService
instead of anAsyncTask
. A service runs in background and is decoupled from the activity life cycle, so you won't be affected by screen orientation change.Put the
AsyncTask
in aFragment
. Fragments have the ability to retain their instances.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: