Android: Activity.onDestroy() is not called when Dalvik kills this Activity

I’m confused about Activity.onDestroy(). I need to free some resources when my Activity is destroyed, but it seems like onDestroy() is called just when i press “Back” key, but not when my Activity is killed by Dalvik. I tested it just by adding log:

Log.v("my_tag", "onDestroy() called");

  • How to call a method in activity from a service
  • MultiDex NoClassDefFound error
  • Generate java code in Eclipse?
  • Setting Up the Licensing Verification Library on Android Studio
  • Android 3.0, Widget Preview
  • Create Notification each day
  • and the same in onCreate() method too:

    Log.v("my_tag", "onCreate() called");

    Then i start my Activity, and i see in logs: onCreate() called. I press “Back” key and then start Activity again, then i see:

    onDestroy() called
    onCreate() called 
    

    Then i press “Home” key and go to my Activity again, logs does not change. All right here.

    Then i press “Home” key again and start some really “heavy” applications. In logs is nothing about onDestroy(), but when i start my Activity again, i see in logs: onCreate() called! So, onDestroy() was not called, but my Activity was killed. What’s wrong?

    Related posts:

    Transfer data through audio jack cable over two Android devices
    Unchecked Call to 'execute(Params…)' as a member of raw type '...
    Confusing Cordova icons and splash screens directory
    How can I find out if a device has a vibrator?
    How can I hide the Android keyboard using JavaScript?
    What is the difference between the SlidingMenu library and the Android Navigation Drawer?
  • Is there a way to reuse builder code for retrofit
  • What functions or codes require GET_TASKS permission in Android?
  • How to get the Latitude and Longitude on Map in Android?
  • How to SHA1 hash a string in Android?
  • Android Studio - How to add a Screen Size Definition
  • onCreate flow continues after finish()
  • One Solution collect form web for “Android: Activity.onDestroy() is not called when Dalvik kills this Activity”

    From the onDestroy() documentation:

    [..] There are situations where the system will simply kill the activity’s
    hosting process without calling this method (or any others) in it, so
    it should not be used to do things that are intended to remain around
    after the process goes away.

    In Android, you won’t get a guaranteed onDestroy() call.
    If you want to release some resources, you should do that in onPause() instead.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.