title: Android destroying activities, killing processes
body: <p>Hi I'm wondering how Android is managing memory and I can't find precise answer anywhere.
  Let's assume I have an application with 5 activities on current activity stack (4 are stopped and 1 is resumed), there is no service connected. I press HOME button so that all of my activities are stopped.
  I start some other memory consuming application and overall device memory is starting to be low. And the question is </p>
  
  <p>... What will happen to my application?</p>
  
  <ol>
  <li>Can system destroy only one or some of my activities to recover memory?</li>
  <li>Will system kill the whole process of my application? Will all activities be nicely destroyed?</li>
  <li>What will happen when I get back to my application when it was totally killed? Will it start from beggining (like the first start) or will it try to recover activities to previeous state / if yes - is it only the one on the top of the stack or all of them?</li>
  </ol>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Before asking this question I've seen Activity lifecycle a few times but it doesn't have answers to my questions.
  I made some tests and I have some answers. "Stop process" in DDMS was a clue for testing.</p>
  
  <p><strong>I haven't tested answer for question 1, but as guide says:</strong></p>
  
  <blockquote>
    <p>If an activity is paused or stopped, the system can drop the activity
    from memory by either asking it to finish, or simply killing its
    process.</p>
  </blockquote>
  
  <p>It seems that one or more of the activities can be destroyed gently(with onDestroy method) without killing the process. You will simply get (onCreate + bundle) when getting back to them.</p>
  
  <p><strong>Question 2 answer:</strong></p>
  
  <p>YES. Generally system kills the whole process this means all data including activities and static fields are destroyed. This is NOT done nicely - you won't get onDestroy or finialize() for any of your paused/stopped activities. This is why saveInstanceState() is called just before onPause method. onPause is basically the last method where you should save something because after this method you could never see onStop or onDestroy. System can just kill the process destroying all of your objects whatever they hold and whatever they are doing.</p>
  
  <p><strong>Question 3 answer:</strong></p>
  
  <p>What will happen when you get back to a killed application?</p>
  
  <ul>
  <li>Prior to Android 2.2 - application will start from the beggining, with launcher activity.</li>
  <li>Starting from 2.2 - system will restore the previous application state. What does it mean? It means that last visible activity will be recreated (onCreate + bundle). What will happen with activity stack? Stack is fine but all activities on it are dead. Each of them will be recreated (onCreate + bundle) when you get back to it with back button.
  There is one more thing about that:</li>
  </ul>
  
  <blockquote>
    <p>Normally, the system clears a task (removes all activities from the
    stack above the root activity) in certain situations when the user
    re-selects that task from the home screen. Typically, this is done if
    the user hasn't visited the task for a certain amount of time, such as
    30 minutes.</p>
  </blockquote>
  
  <p><strong>Conclusion?</strong></p>
  
  <ol>
  <li>Don't think that handling activity rotation problems can be solved
  by android:configChanges="orientation". When you do that you will
  get many other problems that you are not even aware of.</li>
  <li>Test your application with DDMS - Stop process button. <a href="https://stackoverflow.com/questions/11365301/how-to-simulate-android-killing-my-process/">See This</a></li>
  <li>Be careful when using static variables. Don't think that when you initialized them in activity 1 - you will have them initialized in activity 2. The only safe place to initialize global statics would be Application class.</li>
  <li>Remember that you may never see onStop or onDestroy. Close files/databases, stop downloaders in onPause. When you want app to do something in BG - use foreground Service.</li>
  </ol>
  
  <p>That would be it ... Hope I helped with my essey :)</p>