Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>onPause()</code> is always called. This is guaranteed. If you need to save any state in your activity you need to save it in <code>onPause()</code>. <code>onStop()</code> may be called after <code>onPause()</code>, or it may not. Depends on the situation.</p> <p>There are a lot of lifecycle methods. You don't need to override all of them. You only need to override the ones where you need (or want) to customize the behaviour for your activity. There are a lot of lifecycle methods because different applications have different requirements. The lifecycle of an Activity is well-documented and well-behaved. This allows programmers to put the code exactly where it is needed, based on the particular requirements of the application.</p> <p>You have asked</p> <blockquote> <p>What is the good reason for always having onPause() before onStop(). We can do in onStop() what is done in onPause().</p> </blockquote> <p><code>onPause()</code> is always called on your Activity if it is in the foreground when Android wants to do something else. It may start another Activity which may result in your Activity's <code>onStop()</code> getting called. It may just call <code>onResume()</code> on your activity. It may just kill your process without calling any more of your lifecycle methods.</p> <p>Since <code>onStop()</code> is not guaranteed to be called, you can't always <strong>do in onStop() what is done in onPause()</strong>.</p> <p>In most Activities, you will find that you will need to put code in <code>onResume()</code> and <code>onPause()</code>. You usually don't have to do anything in <code>onStop()</code>, <code>onStart()</code> or <code>onRestart()</code>.</p>
 

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