AsyncTask
is a great thing to run complex tasks in another thread.
But when there is an orientation change or another configuration change while the AsyncTask
is still running, the current Activity
is destroyed and restarted. And as the instance of AsyncTask
is connected to that activity, it fails and causes a "force close" message window.
So, I am looking for some kind of "best-practice" to avoid these errors and prevent AsyncTask from failing.
What I've seen so far is:
onRetainNonConfigurationInstance
Activity
is destroyed and restarting it when the Activity
is created again.Some code examples:
Android AsyncTasks during a screen rotation, Part I and Part II
Can you help me to find the best approach which solves the problem best and is easy to implement as well? The code itself is also important as I don't know how to solve this correctly.