title: Best practice: AsyncTask during orientation change
body: <p><code>AsyncTask</code> is a great thing to run complex tasks in another thread.</p>
  
  <p>But when there is an orientation change or another configuration change while the <code>AsyncTask</code> is still running, the current <code>Activity</code> is destroyed and restarted. And as the instance of <code>AsyncTask</code> is connected to that activity, it fails and causes a "force close" message window.</p>
  
  <p>So, I am looking for some kind of "best-practice" to avoid these errors and prevent AsyncTask from failing.</p>
  
  <p>What I've seen so far is:</p>
  
  <ul>
  <li>Disable orientation changes.(For sure not the way you should handle this.)</li>
  <li>Letting the task survive and updating it with the new activity instance via <code>onRetainNonConfigurationInstance</code></li>
  <li>Just canceling the task when the <code>Activity</code> is destroyed and restarting it when the <code>Activity</code> is created again.</li>
  <li>Binding the task to the application class instead of the activity instance.</li>
  <li>Some method used in the "shelves" project (via onRestoreInstanceState)</li>
  </ul>
  
  <p>Some code examples:</p>
  
  <p><a href="http://fattybeagle.com/2011/02/14/android-asynctasks-during-a-screen-rotation-part-i/" rel="noreferrer">Android AsyncTasks during a screen rotation, Part I</a> and <a href="http://fattybeagle.com/2011/02/15/android-asynctasks-during-a-screen-rotation-part-ii/" rel="noreferrer">Part II</a></p>
  
  <p><a href="http://code.google.com/p/shelves/source/browse/trunk/Shelves/src/org/curiouscreature/android/shelves/activity/ShelvesActivity.java" rel="noreferrer">ShelvesActivity.java</a></p>
  
  <p>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.</p>