Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can have some more information here : <a href="http://developer.android.com/training/basics/activity-lifecycle/stopping.html" rel="nofollow noreferrer">http://developer.android.com/training/basics/activity-lifecycle/stopping.html</a></p> <p>Even if I think you already read it because you already study the activity lifecycle, you can see in the first figure that the <code>onDestroy()</code> is called after the <code>onStop()</code> and this call is totally managed by the system : you shouldn't expect any behavior. The system will decide itself WHEN to call this method, and sometimes, this method will never be called (see here : <a href="http://developer.android.com/reference/android/app/Activity.html" rel="nofollow noreferrer">http://developer.android.com/reference/android/app/Activity.html</a>). When system needs memory, your activity will pass in <code>onStop()</code> and nothing more.</p> <p>So, to answer your second question, you shloud read the note in the documentation about the <code>onDestroy()</code> method :</p> <blockquote> <p>Note: <strong>do not count on this method being called as a place for saving data!</strong> For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. <strong>This method is usually implemented to free resources like threads</strong> that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. 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.</p> </blockquote> <p>So it's pretty clear that it's a bad place to make your clean-up process. So you shloud use one of <code>onPause()</code> or <code>onStop()</code> method.</p> <p><code>onStop()</code> is described like this in the documentation :</p> <blockquote> <p>Called when you are no longer visible to the user. You will next receive either <code>onRestart()</code>, <code>onDestroy()</code>, or nothing, depending on later user activity.</p> </blockquote> <p><code>onPause()</code> is described like this in the documentation :</p> <blockquote> <p>Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. [...] When activity B is launched in front of activity A, this callback will be invoked on A. <strong>B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.</strong> [...] <strong>In situations where the system needs more memory it may kill paused processes to reclaim resources.</strong></p> </blockquote> <p>We now know that <code>onPause()</code> is designed to allow you to save your data, and also that after <code>onPause()</code> was executed, the system <em>could</em> kill your process. So, making the clean-up in <code>onPause()</code> seems to be the safest place as you're pretty sure it will be called everytime.</p> <p>Also, as you can read, making your clean up here can make your app slow, but anyway cleaning and recreating your database at each gain/loose of focus is a really heavy process...</p> <p>To resume : make your clean up process in the <code>onPause()</code> method and your init process in the <code>onResume()</code>. <strong>Keep it mind that your application can be really slow with this kind of process.</strong></p> <p>Hope this can help you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload