title: Is it safe to do all cleaning up in onDestroy?
body: <p>More concretely: Is it safe to place the canceling of a task in onDestroy? Also, is it safe to use onDestroy for unregistering receivers and freeing up resources?</p>
  
  <p>My aim is to make sure that my task is canceled/destroyed when the Activity is destroyed, but not before.</p>
  
  <p>onDestroy():</p>
  
  <ol>
  <li>is called when the activity is destroyed and resources must be
  released.</li>
  <li>is NOT called when the activity is destroyed in a hurry (when the
  system is low on resources etc).</li>
  </ol>
  
  <p>The first case is clear: I do all cleaning in onDestroy and no problems arise. The second case is a bit of a problem though. When the Activity is destroyed and onDestroy is skipped (so I don't cancel my task), could it happen that the task continues execution, then completes and tries to update the dead Activity, so the app crashes?</p>
  
  <hr>
  
  <p>We come to the real question:</p>
  
  <ol>
  <li><strong>When an Activity is killed and onDestroy is skipped, is everything attached to that Activity automatically destroyed?</strong> (Is onDestroy skipped <strong>only in case</strong> that everything will be wiped out altogether? Tasks, registered receivers etc)</li>
  <li>If onDestroy is skipped does this mean that the <strong>whole app</strong> is being killed?</li>
  </ol>
  
  <hr>
  
  <p>Let's focus on onDestroy(), because the solution is not in onPause() or onStop(). Arguments:</p>
  
  <ul>
  <li>onStop() could be skipped when the Activity is being destroyed, just like onDestroy</li>
  <li>onPause is called too early and too often, so it is not appropriate for the use case. Examples:</li>
  </ul>
  
  <p><strong>Screen lock</strong>: onPause can be called when the device <strong>screen is locked</strong>. Very often this happens like a screensaver and the user unlocks immediately because he is standing there looking at the screen. Canceling tasks and stopping everything my app is doing in such a case will only degrade user experience. I don't want my app to choke and misbehave just because of an incidental "screensaver".</p>
  
  <p>In an example app I have two screens that are Activities. The user can quickly switch between them. In this app users tend to switch screens often and quickly. </p>
  
  <p><strong>Navigation</strong>: One of the screens has a map which receives location updates from the system. It records a precise graphical log of the changes in location (route), so it needs to run constantly until the Activity is closed. Normally I would register and unregister any receivers in onResume and onPause. However, this would make the app very unusable, as the updates on the map will stop every time the user navigates away. Therefore, I would like to unregister the receivers in onDestroy.</p>
  
  <p><strong>Loading list</strong>: The second screen has a list that shows data from a webservice. It takes 4 seconds to download the data. I use an AsyncTask and I know I should cancel when necessary. It should not be canceled in onPause, because it should continue loading while the user switches between screens. Therefore, I would like to cancel it in onDestroy.</p>
  
  <p>There can be many more examples. Some of them might not be totally appropriate in everyone's opinion (you might even suggest using a service instead of AsyncTask). But the idea is important, and all of them have the same <strong>idea</strong>: keep on doing work that's specific to the Activity, while the Activity is <em>paused</em>, but ENSURE to stop doing it when the Activity is <em>destroyed</em>. (It does not matter whether I am using an AsyncTask or a Service. In either case, the work should be stopped when the Activity is destroyed.)</p>
  
  <p>P.S. If the answer is that <em>it is not safe to do the clean up in onDestroy</em>, this would mean that the Android framework requires us to stop everything we are doing in onPause. And then I would not see any reason for using onDestroy...</p>