1. Welcome to the newer, better version of Android Forums! Be sure to see the announcement and enjoy checking it out!

    Some of you have been having login issues. - Please try now. Sorry for the trouble!
  2. All attachments uploaded on the first day of this new look need to be re-uploaded, or will appear broken. All prior to that, and all going forward, should work fine. We apologize for the inconvenience!

Will the onStop and onDestroy always get called?


  1. narke

    narke Member

    Hi,

    There are some process manage application that allows user stop any running aplication by a button pressing. I want to know, in these situations, the onStop and onDestroy methods will always get executed?

    Thanks in advance.
    -narke

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    onStop and onDestroy should ALWAYS get called.
  3. ChrisJD

    ChrisJD New Member

    That isn't correct. I don't think I can link to it because I haven't made enough posts yet but the relevant section of the docs is the API reference for android.app.Activity.

    And the table in the above mentioned page indicates that the process can be killed in those two states.

    If the process is killed rather than being asked to finish those methods will not get called. That's why the docs tell you to save all the info you need to restore an activity during onPause.

    How the applications the OP refers to behave is going to depend on how they terminate a running application. Do they ask nicely somehow or do they just kill the process? In any case your app shouldn't be reliant on onStop or onDestroy being called.
  4. narke

    narke Member

    Thanks Grise. If so, I think the onStoo and onDestroy are not of much use, do you agree? But I'd rather believe what you said is the right version.

    The process management application I was talking about particularly is the famous ES process manager.

    -narke
  5. jonbonazza

    jonbonazza Well-Known Member

    Even if it is killed by the OS, onStop and onDestroy should still be called.
  6. ChrisJD

    ChrisJD New Member

    The quoted section is on the following page below the large table near the top of the page. Activity | Android Developers Short version: Pre-honeycomb onStop and onDestroy aren't guaranteed to run. Honeycomb onwards onDestroy isn't guaranteed to run.

    narke: While you should be saving the state of your app in onPause the onStop and onDestroy methods are still useful.

    Use onStop or onDestroy to make sure any threads you've started are stopped, message queues are cleared if your posting delayed messages between threads and any other cleanup that needs to be done so you don't leak views/threads/the whole activity.

    If the whole process is killed none of these things matter as the whole process is wiped out.
    Kostas G and jonbonazza like this.
  7. jonbonazza

    jonbonazza Well-Known Member

    I stand corrected. Thanks for the clarification. =)

Share This Page