Search code examples
androidandroid-lifecycleandroid-ondestroy

Is onDestroy() method always called only by OS?


I am beginning to learn android development in two video courses I have watched and one book I have read it said onDestroy() is called by OS when it feels to freeup memory. Nobody even mentioned that it will be called when user closes the app. That created me doubt if onDestroy() is called when the app is closed by the user.

Someone please clarify.


Solution

  • When a user switches away from an app in Android, the onPause() is called, followed by onStop(). At this point, the app is no longer visible to the user and is suspended, but still available to be resumed.

    If the OS decides to terminate the app, when it does, onDestroy() will be called. That may be a few seconds after onStop, or many hours or days later.

    When the user navigates back to the app, if onDestroy() was never called, then the app was never terminated, and onCreate() will not be called either. onStart() and onResume() will always be called.