<p>This tutorial is about Activity in Android, Activity Lifecycle, Real example use-cases of Activity Lifecycle.</p> 
 
 
 
<h3><span style="color: #000080;"><strong>Activity</strong></span></h3> 
 
 
 
<p>In Android, an <span style="color: #0000ff;"><strong>Activity</strong></span> is referred to as one screen in an application. An activity provides the window in which the app draws its UI. An Android app consists of one or more screens or activities.</p> 
<p class="p1"><span style="color: #000080;"><b>For example</b></span>, when we open our Gmail application, then we see our emails on the screen. Those emails are present in an Activity. If we open some particular email, then that email will be opened in some other Activity.</p> 
<p class="p1">Activities in the system are managed as <a href="https://developer.android.com/guide/components/activities/tasks-and-back-stack"><span class="s1"><span style="color: #0000ff;"><strong>activity stacks</strong></span></span></a>. As a user navigates throughout an app, Android maintains the visited activities in a stack, with the currently visible activity always placed at the top of the stack.</p> 
<h3><span style="color: #000080;"><strong>Activity Lifecycle</strong></span></h3> 
 
 
 
<p>An Android activity undergoes through a number of states during its whole lifecycle.</p> 
<p>An activity has essentially four states:</p> 
<p class="p1"><span style="color: #0000ff;"><strong>Running</strong></span> : Activity is visible and interacting with the user.</p> 
<p class="p1"><strong><span style="color: #0000ff;">Paused </span></strong>: Activity is still visible, but no longer interacting with the user.</p> 
<p class="p1"><span style="color: #0000ff;"><strong>Stopped </strong></span>: Activity is no longer visible.</p> 
<p class="p1"><span style="color: #0000ff;"><strong>Killed </strong></span>: Activity has been killed by the system (low memory) or its <strong><span style="color: #008000;">finish()</span></strong> method has been called.</p> 
<p>To handle these states we should override these seven callbacks <span style="color: #008000;"><strong>onCreate, onStart, onStop, onPause, onResume, onRestart, </strong><span style="color: #000000;">and</span><strong> onDestroy</strong></span> in our Activity.</p> 
<p>The following diagram shows the whole Activity lifecycle:</p> 
<p><img class="wp-image-2581 aligncenter" src="https://c1ctech.com/wp-content/uploads/2021/04/activity_lifecycle.png" alt="" width="588" height="757" /></p> 
<h3><span style="color: #000080;"><strong>Activity Lifecycle Methods</strong></span></h3> 
<p>The Activity lifecycle consists of 7 methods:</p> 
 
 
 
<h5><span style="color: #0000ff;"><strong>onCreate()</strong></span></h5> 
<ul class="wp-block-list"> 
<li class="p1">Called when the activity is first created or the first method that gets called when a user first opens an activity.</li> 
<li class="p1">This is where you should do all of your normal static setups: create views, bind data to lists, etc.</li> 
<li class="p1">This method also provides you with a <strong><span style="color: #0000ff;">Bundle</span></strong> containing the activity&#8217;s previously frozen state, if there was one.</li> 
<li class="p1">Always followed by <span style="color: #008000;"><strong><span class="s1">onStart()</span></strong></span>.</li> 
</ul> 
<h5><span style="color: #0000ff;"><strong>onRestart()</strong></span></h5> 
<ul> 
<li> 
<p class="p1">Called after <span style="color: #008000;"><strong><a style="color: #008000;" href="https://developer.android.com/reference/android/app/Activity#onStop()"><span class="s1">onStop()</span></a></strong></span> when the current activity is being re-displayed to the user (the user has navigated back to it).</p> 
</li> 
<li> 
<p class="p1">It will be followed by <strong><span style="color: #008000;"><a style="color: #008000;" href="https://developer.android.com/reference/android/app/Activity#onStart()"><span class="s1">onStart()</span></a></span></strong> and then <span style="color: #008000;"><strong><a style="color: #008000;" href="https://developer.android.com/reference/android/app/Activity#onResume()"><span class="s1">onResume()</span></a></strong></span>.</p> 
</li> 
<li> 
<p class="p1">For activities that are using raw <span style="color: #008000;"><strong><a style="color: #008000;" href="https://developer.android.com/reference/android/database/Cursor"><span class="s1">Cursor</span></a></strong></span> objects, this is usually the place where the cursor should be requeried (because you had deactivated it in <span style="color: #008000;"><strong><a style="color: #008000;" href="https://developer.android.com/reference/android/app/Activity#onStop()"><span class="s1">onStop()</span></a></strong></span>.</p> 
</li> 
</ul> 
<h5><span style="color: #0000ff;"><strong>onStart()</strong></span></h5> 
<ul> 
<li class="p1">Called when the activity is becoming visible to the user (but not ready for user interaction). </li> 
<li> 
<p class="p1">Called after <span style="color: #008000;"><strong><a style="color: #008000;" href="https://developer.android.com/reference/android/app/Activity#onCreate(android.os.Bundle)"><span class="s1">onCreate(Bundle)</span></a></strong></span> or after <span style="color: #008000;"><strong><a style="color: #008000;" href="https://developer.android.com/reference/android/app/Activity#onRestart()"><span class="s1">onRestart()</span></a> </strong></span>when the activity had been stopped but is now again being displayed to the user.</p> 
</li> 
<li> 
<p class="p1">Followed by <span style="color: #008000;"><strong><span class="s1">onResume()</span></strong></span> if the activity comes to the foreground, or <span style="color: #008000;"><strong><span class="s1">onStop()</span></strong></span> if you call <strong><span style="color: #008000;"><a style="color: #008000;" href="https://developer.android.com/reference/android/app/Activity#finish()">finish()</a></span></strong> from within this function.</p> 
</li> 
<li>It&#8217;s a good place to put a broadcastReceiver or initialize some state about the UI that should display consistently anytime the user comes back to this activity or to begin drawing visual elements, running animations, etc.</li> 
</ul> 
<h5><span style="color: #0000ff;"><strong>onResume()</strong></span></h5> 
<ul> 
<li class="p1">Called when the activity will start interacting with the user.</li> 
<li class="p1">At this point, your activity is at the top of its activity stack and ready to receive input.</li> 
<li class="p1">Always followed by <span style="color: #008000;"><strong><span class="s1">onPause()</span></strong></span>.</li> 
<li>This is a good place to try to open exclusive-access devices or to get access to singleton resources on platform versions prior to <strong><span style="color: #008000;"><code dir="ltr" translate="no"><a style="color: #008000;" href="https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q">Build.</a></code><code dir="ltr" translate="no"><a style="color: #008000;" href="https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q">VERSION_CODES</a></code><code dir="ltr" translate="no"><a style="color: #008000;" href="https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q">.Q</a></code></span></strong> .</li> 
</ul> 
<h5><span style="color: #0000ff;"><strong>onPause()</strong></span></h5> 
<ul> 
<li>Called when the activity loses foreground state, is no longer focusable or before transition to stopped/hidden or destroyed state.</li> 
<li>The activity is still visible to user, so it&#8217;s recommended to keep it visually active and continue updating the UI.</li> 
<li>Implementations of this method must be very quick (not do a lengthy operation or stop things that consume a noticeable amount of CPU) because the next activity will not be resumed until this method returns.</li> 
<li> 
<p>This is a good place to try to close exclusive-access devices or to release access to singleton resources o<span style="font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;">n platform versions prior to </span><span style="color: #008000;"><strong><code dir="ltr" translate="no"><a style="color: #008000;" href="https://developer.android.com/reference/android/os/Build.VERSION_CODES#Q">Build.VERSION_CODES.Q</a></code></strong></span><span style="font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;"> </span></p> 
</li> 
<li>Followed by either <span style="color: #008000;"><strong>onResume()</strong></span> if the activity returns back to the front, or <span style="color: #008000;"><strong>onStop()</strong></span> if it becomes invisible to the user.</li> 
</ul> 
<h5><span style="color: #0000ff;"><strong>onStop()</strong></span></h5> 
<ul> 
<li class="p1">Called when the activity is no longer visible to the user.</li> 
<li class="p1">This may happen either because a new activity is being started on top, an existing one is being brought in front of this one, or this one is being destroyed.</li> 
<li>This is a good place to stop refreshing UI, running animations, and other visual things.</li> 
<li class="p1">Followed by either <span style="color: #008000;"><strong><span class="s1">onRestart()</span></strong></span> if this activity is coming back to interact with the user, or <span style="color: #008000;"><strong><span class="s1">onDestroy()</span></strong></span> if this activity is going away.</li> 
</ul> 
<h5><span style="color: #0000ff;"><strong>onDestroy()</strong></span></h5> 
<ul> 
<li class="p1">called before the activity is destroyed.</li> 
<li class="p1"> 
<p>The system invokes this callback either because:</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>
 
<ol> 
<li>the activity is finishing (due to the user completely dismissing the activity or due to <span style="color: #008000;"><strong><a style="color: #008000;" href="https://developer.android.com/reference/android/app/Activity#finish()">finish()</a></strong></span> being called on the activity), or</li> 
<li>the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)</li> 
</ol> 
</li> 
<li> 
<p>This is where the lifecycle components can clean up anything it needs to before the Activity is destroyed.</p> 
</li> 
</ul> 
<h3 class="graf--h3"><span style="color: #000080;"><strong>Activity Lifecycle Use Cases </strong></span></h3> 
<p class="graf--p">Now, let&#8217;s see real-life use-cases to understand the lifecycle of an activity.</p> 
<h5><span style="color: #0000ff;"><b class="graf--bold">UseCase 1: </b><strong>Open an Activity and press the Back button</strong></span></h5> 
<p class="graf--p">When you <strong>first time opens</strong> an app or activity, <span style="color: #008000;"><strong>onCreate</strong></span>, <strong><span style="color: #008000;">onStart,</span> </strong>and <strong><span style="color: #008000;">onResume</span> </strong>callbacks will be triggered. </p> 
<pre class="graf--p"><code class="language-java graf graf--code hljs">onCreate ->; onStart ->; onResume</code></pre> 
<p class="graf--p">Now <span style="color: #008000;"><strong>onPause</strong></span>, <strong><span style="color: #008000;">onStop</span> </strong>and <strong><span style="color: #008000;">onDestroy</span> </strong>callbacks have triggered, as soon as we <span style="color: #008000;"><strong>press the <span style="color: #0000ff;">back</span></strong></span> button.</p> 
<pre><code class="language-java graf graf--code hljs">onPause ->; onStop ->; onDestroy </code></pre> 
<h5><span style="color: #0000ff;"><strong>UseCase 2: Open an activity and press the Home button</strong></span></h5> 
<p class="p1">If we press the <span style="color: #008000;"><b>Home button</b></span>, our app will be minimized. But it will not be killed. So rather than <span style="color: #008000;"><strong>onDestroy</strong></span> method call, <b><span style="color: #008000;">onPause</span> </b>and <b><span style="color: #008000;">onStop</span> </b>will be called.</p> 
<pre><code class="language-java graf graf--code hljs">onPause ->; onStop </code></pre> 
<p>Now If we reopen the activity again, <strong><span style="color: #008000;">onRestart</span></strong>, <strong><span style="color: #008000;">onStart</span></strong>, and <strong><span style="color: #008000;">onResume</span></strong> callbacks will be triggered.</p> 
<pre><code class="language-java graf graf--code hljs">onRestart ->;onStart ->; onResume </code></pre> 
<h5 class="p1"><span style="color: #0000ff;"><strong>UseCase 3: Open an Activity and Lock the Screen</strong></span></h5> 
<p class="p1">If we simply turn off the screen (press the power button) while using the app, <span style="color: #008000;"><strong>onPause</strong></span> and <span style="color: #008000;"><strong>onStop</strong></span> methods are called. </p> 
<pre><code class="language-java graf graf--code hljs">onPause ->; onStop </code></pre> 
<p class="p1">If we unlock the screen, <span style="color: #008000;"><strong>onRestart</strong></span>, <span style="color: #008000;"><strong>onStart</strong></span>, and <strong><span style="color: #008000;">onResume</span></strong> callbacks will be triggered.</p> 
<pre><code class="language-java graf graf--code hljs"></code><code class="language-java graf graf--code hljs">onRestart ->;onStart ->; onResume </code></pre> 
<h5><span style="color: #0000ff;"><strong>UseCase 4: Open an Activity and Phone Ringing</strong></span></h5> 
<p class="p1">Now if an incoming call comes while running activity and if we receive that call, in this case, <span style="color: #008000;"><strong>onPause</strong></span> and <strong><span style="color: #008000;">onStop</span></strong> callbacks will be triggered.</p> 
<pre><code class="language-java graf graf--code hljs">onPause ->; onStop </code></pre> 
<p class="p1">If we disconnect the call, <span style="color: #008000;"><strong>onRestart</strong></span>, <span style="color: #008000;"><strong>onStart</strong></span>, and <strong><span style="color: #008000;">onResume</span></strong> callbacks will be triggered.</p> 
<pre><code class="language-java graf graf--code hljs">onRestart ->; onStart ->; onResume </code></pre> 
<h5 class="graf--p"><span style="color: #0000ff;"><b class="graf--bold">UseCase 5: Kill the app from the recent app&#8217;s tray</b></span></h5> 
<p class="graf--p">When we kill the app from the recent app&#8217;s tray, <span style="color: #008000;"><strong>onPause</strong></span>, <span style="color: #008000;"><strong>onStop </strong></span>and <span style="color: #008000;"><strong>onDestroy </strong></span>callbacks will be triggered. </p> 
<pre><code class="language-java graf graf--code hljs">onPause ->; onStop ->; onDestroy </code></pre> 
<p class="graf--p">Here <strong><span style="color: #008000;">onDestroy</span></strong> will kill the instance of the activity. So now when we reopen the activity, it will call <span style="color: #008000;"><strong>onCreate</strong></span> and not <strong><span style="color: #008000;">onRestart</span></strong> to start the activity.</p> 
<h5 class="graf--p"><span style="color: #0000ff;"><b class="graf--bold">UseCase 6: Move from one activity to another</b></span></h5> 
<p class="graf--p">In this use case, we will see what happens when we move from one activity to another <b class="graf--bold">(</b>let&#8217;s say <span style="color: #008000;"><strong>Activity A to Activity B</strong></span><i class="graf--italic">)</i>. </p> 
<p class="graf--p"><span class="graf--italic">When </span><span style="color: #0000ff;"><strong>Activity A</strong></span> will open, the following states are called initially,</p> 
<pre><code class="language-java graf graf--code hljs">onCreate ->; onStart ->; onResume</code></pre> 
<p class="graf--p">Let&#8217;s say on click of a button we opened <span style="color: #0000ff;"><strong>Activity B</strong></span>. While opening <strong><span style="color: #0000ff;">Activity B</span></strong>, first, <strong><span style="color: #008000;">onPause</span></strong> will be called for <strong><span style="color: #0000ff;">Activity A</span></strong> and then,</p> 
<pre><code class="language-java graf graf--code hljs">onCreate ->; onStart ->; onResume</code></pre> 
<p class="graf--p">will be called for <span style="color: #0000ff;"><strong>Activity B</strong></span> and then finally <strong><span style="color: #008000;">onStop</span></strong> of <span style="color: #0000ff;"><strong>Activity A</strong></span> will be called.</p> 
<p class="graf--p">Now, when we press the back button from <strong><span style="color: #0000ff;"><i class="graf--italic">Activity B</i> </span></strong>to <strong><span style="color: #0000ff;">Activity A</span></strong>, then first, <strong><span style="color: #008000;">onPause</span></strong> of <strong><span style="color: #0000ff;"><i class="graf--italic">Activity B</i></span></strong> is called and then,</p> 
<pre><code class="language-java graf graf--code hljs">onRestart ->; onStart ->; onResume</code></pre> 
<p class="graf--p">gets called for <span style="color: #0000ff;"><strong>Activity A</strong></span> and it is displayed to the user. Here you can see <strong><span style="color: #008000;">onRestart</span></strong> gets called rather than <span style="color: #008000;"><strong>onCreate</strong></span> as it is restarting the activity and not creating it.</p> 
<p class="graf--p">Then after <strong><span style="color: #008000;">onResume</span></strong> of <span style="color: #0000ff;"><strong>Activity A</strong></span> is called, the following methods of <span style="color: #0000ff;"><strong>Activity B</strong></span> are called and now <strong><span style="color: #0000ff;">Activity B</span></strong> is destroyed (removed from activity stack).</p> 
<pre><code class="language-java graf graf--code hljs">onStop ->; onDestroy </code></pre> 
<h5 class="graf--p"><span style="color: #0000ff;"><b class="graf--bold">UseCase 7: Activity&#8217;s orientation change</b></span></h5> 
<p>When we change the orientation of an Activity while using the app, the following methods get called.</p> 
<pre><code class="language-java graf graf--code hljs">onPause ->; onStop ->; onDestroy </code></pre> 
<p>After <span style="color: #008000;"><strong>onDestroy</strong></span>, the instance of an Activity gets destroyed and a new instance of activity gets created by calling the following methods.</p> 
<pre><code class="language-java graf graf--code hljs">onCreate ->; onStart ->; onResume</code></pre> 
<p> ;</p> 


