Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()? What is its purpose?</p> </blockquote> <p>OK, as my first answer was pretty long I won't extend it further so let's try this...</p> <pre><code>public DriveToWorkActivity extends Activity implements onReachedGroceryStoreListener { } public GroceryStoreActivity extends Activity {} </code></pre> <p><strong>PLEASE NOTE:</strong> I've deliberately left out the calls to things like <code>super.onCreate(...)</code> etc. This is pseudo-code so give me some artistic licence here. ;)</p> <p>The methods for <code>DriveToWorkActivity</code> follow...</p> <pre><code>protected void onCreate(...) { openGarageDoor(); unlockCarAndGetIn(); closeCarDoorAndPutOnSeatBelt(); putKeyInIgnition(); } protected void onStart() { startEngine(); changeRadioStation(); switchOnLightsIfNeeded(); switchOnWipersIfNeeded(); } protected void onResume() { applyFootbrake(); releaseHandbrake(); putCarInGear(); drive(); } protected void onPause() { putCarInNeutral(); applyHandbrake(); } protected void onStop() { switchEveryThingOff(); turnOffEngine(); removeSeatBeltAndGetOutOfCar(); lockCar(); } protected void onDestroy() { enterOfficeBuilding(); } protected void onReachedGroceryStore(...) { Intent i = new Intent(ACTION_GET_GROCERIES, ..., this, GroceryStoreActivity.class); } protected void onRestart() { unlockCarAndGetIn(); closeDoorAndPutOnSeatBelt(); putKeyInIgnition(); } </code></pre> <p>OK, so it's another long one (sorry folks). But here's my explanation...</p> <p><code>onResume()</code> is when I start driving and <code>onPause()</code> is when I come to a temporary stop. So I drive then reach a red light so I pause...the light goes green and I resume. Another red light and I pause, then green so I resume. The <code>onPause() -&gt; onResume() -&gt; onPause() -&gt; onResume()</code> loop is a tight one and occurs many times through my journey.</p> <p>The loop from being stopped back through a restart (preparing to carry on my journey) to starting again is perhaps less common. In one case, I spot the Grocery Store and the <code>GroceryStoreActivity</code> is started (forcing my <code>DriveToWorkActivity</code> to the point of <code>onStop()</code>). When I return from the store, I go through <code>onRestart()</code> and <code>onStart()</code> then resume my journey.</p> <p>I could put the code that's in <code>onStart()</code> into both <code>onCreate()</code> and <code>onRestart()</code> and not bother to override <code>onStart()</code> at all but the more that needs to be done between <code>onCreate() -&gt; onResume()</code> and <code>onRestart() -&gt; onResume()</code>, the more I'm duplicating things.</p> <p>So, to requote once more...</p> <blockquote> <p>Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()?</p> </blockquote> <p>If you don't override <code>onStart()</code> then this is effectively what happens. Although the <code>onStart()</code> method of <code>Activity</code> will be called implicitly, the effect in your code is effectively <code>onCreate() -&gt; onResume()</code> or <code>onRestart() -&gt; onResume()</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