Site icon C1CTech

Activity Lifecycle in Android

&NewLine;<p>This tutorial is about Activity in Android&comma; Activity Lifecycle&comma; Real example use-cases of Activity Lifecycle&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Activity<&sol;strong><&sol;span><&sol;h3> &NewLine; &NewLine; &NewLine; &NewLine;<p>In Android&comma; an <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Activity<&sol;strong><&sol;span> is referred to as one screen in an application&period; An activity provides the window in which the app draws its UI&period; An Android app consists of one or more screens or activities&period;<&sol;p> &NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>For example<&sol;b><&sol;span>&comma; when we open our Gmail application&comma; then we see our emails on the screen&period; Those emails are present in an Activity&period; If we open some particular email&comma; then that email will be opened in some other Activity&period;<&sol;p> &NewLine;<p class&equals;"p1">Activities in the system are managed as <a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;guide&sol;components&sol;activities&sol;tasks-and-back-stack"><span class&equals;"s1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity stacks<&sol;strong><&sol;span><&sol;span><&sol;a>&period; As a user navigates throughout an app&comma; Android maintains the visited activities in a stack&comma; with the currently visible activity always placed at the top of the stack&period;<&sol;p> &NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Activity Lifecycle<&sol;strong><&sol;span><&sol;h3> &NewLine; &NewLine; &NewLine; &NewLine;<p>An Android activity undergoes through a number of states during its whole lifecycle&period;<&sol;p> &NewLine;<p>An activity has essentially four states&colon;<&sol;p> &NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Running<&sol;strong><&sol;span> &colon; Activity is visible and interacting with the user&period;<&sol;p> &NewLine;<p class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Paused <&sol;span><&sol;strong>&colon; Activity is still visible&comma; but no longer interacting with the user&period;<&sol;p> &NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Stopped <&sol;strong><&sol;span>&colon; Activity is no longer visible&period;<&sol;p> &NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Killed <&sol;strong><&sol;span>&colon; Activity has been killed by the system &lpar;low memory&rpar; or its <strong><span style&equals;"color&colon; &num;008000&semi;">finish&lpar;&rpar;<&sol;span><&sol;strong> method has been called&period;<&sol;p> &NewLine;<p>To handle these states we should override these seven callbacks <span style&equals;"color&colon; &num;008000&semi;"><strong>onCreate&comma; onStart&comma; onStop&comma; onPause&comma; onResume&comma; onRestart&comma; <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">and<&sol;span><strong> onDestroy<&sol;strong><&sol;span> in our Activity&period;<&sol;p> &NewLine;<p>The following diagram shows the whole Activity lifecycle&colon;<&sol;p> &NewLine;<p><img class&equals;"wp-image-2581 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;04&sol;activity&lowbar;lifecycle&period;png" alt&equals;"" width&equals;"588" height&equals;"757" &sol;><&sol;p> &NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Activity Lifecycle Methods<&sol;strong><&sol;span><&sol;h3> &NewLine;<p>The Activity lifecycle consists of 7 methods&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onCreate&lpar;&rpar;<&sol;strong><&sol;span><&sol;h5> &NewLine;<ul class&equals;"wp-block-list"> &NewLine;<li class&equals;"p1">Called when the activity is first created or the first method that gets called when a user first opens an activity&period;<&sol;li> &NewLine;<li class&equals;"p1">This is where you should do all of your normal static setups&colon; create views&comma; bind data to lists&comma; etc&period;<&sol;li> &NewLine;<li class&equals;"p1">This method also provides you with a <strong><span style&equals;"color&colon; &num;0000ff&semi;">Bundle<&sol;span><&sol;strong> containing the activity&&num;8217&semi;s previously frozen state&comma; if there was one&period;<&sol;li> &NewLine;<li class&equals;"p1">Always followed by <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">onStart&lpar;&rpar;<&sol;span><&sol;strong><&sol;span>&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onRestart&lpar;&rpar;<&sol;strong><&sol;span><&sol;h5> &NewLine;<ul> &NewLine;<li> &NewLine;<p class&equals;"p1">Called after <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Activity&num;onStop&lpar;&rpar;"><span class&equals;"s1">onStop&lpar;&rpar;<&sol;span><&sol;a><&sol;strong><&sol;span> when the current activity is being re-displayed to the user &lpar;the user has navigated back to it&rpar;&period;<&sol;p> &NewLine;<&sol;li> &NewLine;<li> &NewLine;<p class&equals;"p1">It will be followed by <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Activity&num;onStart&lpar;&rpar;"><span class&equals;"s1">onStart&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> and then <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Activity&num;onResume&lpar;&rpar;"><span class&equals;"s1">onResume&lpar;&rpar;<&sol;span><&sol;a><&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<&sol;li> &NewLine;<li> &NewLine;<p class&equals;"p1">For activities that are using raw <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;database&sol;Cursor"><span class&equals;"s1">Cursor<&sol;span><&sol;a><&sol;strong><&sol;span> objects&comma; this is usually the place where the cursor should be requeried &lpar;because you had deactivated it in <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Activity&num;onStop&lpar;&rpar;"><span class&equals;"s1">onStop&lpar;&rpar;<&sol;span><&sol;a><&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onStart&lpar;&rpar;<&sol;strong><&sol;span><&sol;h5> &NewLine;<ul> &NewLine;<li class&equals;"p1">Called when the activity is becoming visible to the user &lpar;but not ready for user interaction&rpar;&period; <&sol;li> &NewLine;<li> &NewLine;<p class&equals;"p1">Called after <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Activity&num;onCreate&lpar;android&period;os&period;Bundle&rpar;"><span class&equals;"s1">onCreate&lpar;Bundle&rpar;<&sol;span><&sol;a><&sol;strong><&sol;span> or after <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Activity&num;onRestart&lpar;&rpar;"><span class&equals;"s1">onRestart&lpar;&rpar;<&sol;span><&sol;a> <&sol;strong><&sol;span>when the activity had been stopped but is now again being displayed to the user&period;<&sol;p> &NewLine;<&sol;li> &NewLine;<li> &NewLine;<p class&equals;"p1">Followed by <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">onResume&lpar;&rpar;<&sol;span><&sol;strong><&sol;span> if the activity comes to the foreground&comma; or <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">onStop&lpar;&rpar;<&sol;span><&sol;strong><&sol;span> if you call <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Activity&num;finish&lpar;&rpar;">finish&lpar;&rpar;<&sol;a><&sol;span><&sol;strong> from within this function&period;<&sol;p> &NewLine;<&sol;li> &NewLine;<li>It&&num;8217&semi;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&comma; running animations&comma; etc&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onResume&lpar;&rpar;<&sol;strong><&sol;span><&sol;h5> &NewLine;<ul> &NewLine;<li class&equals;"p1">Called when the activity will start interacting with the user&period;<&sol;li> &NewLine;<li class&equals;"p1">At this point&comma; your activity is at the top of its activity stack and ready to receive input&period;<&sol;li> &NewLine;<li class&equals;"p1">Always followed by <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">onPause&lpar;&rpar;<&sol;span><&sol;strong><&sol;span>&period;<&sol;li> &NewLine;<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&equals;"color&colon; &num;008000&semi;"><code dir&equals;"ltr" translate&equals;"no"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;os&sol;Build&period;VERSION&lowbar;CODES&num;Q">Build&period;<&sol;a><&sol;code><code dir&equals;"ltr" translate&equals;"no"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;os&sol;Build&period;VERSION&lowbar;CODES&num;Q">VERSION&lowbar;CODES<&sol;a><&sol;code><code dir&equals;"ltr" translate&equals;"no"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;os&sol;Build&period;VERSION&lowbar;CODES&num;Q">&period;Q<&sol;a><&sol;code><&sol;span><&sol;strong> &period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onPause&lpar;&rpar;<&sol;strong><&sol;span><&sol;h5> &NewLine;<ul> &NewLine;<li>Called when the activity loses foreground state&comma; is no longer focusable or before transition to stopped&sol;hidden or destroyed state&period;<&sol;li> &NewLine;<li>The activity is still visible to user&comma; so it&&num;8217&semi;s recommended to keep it visually active and continue updating the UI&period;<&sol;li> &NewLine;<li>Implementations of this method must be very quick &lpar;not do a lengthy operation or stop things that consume a noticeable amount of CPU&rpar; because the next activity will not be resumed until this method returns&period;<&sol;li> &NewLine;<li> &NewLine;<p>This is a good place to try to close exclusive-access devices or to release access to singleton resources o<span style&equals;"font-size&colon; 16px&semi; font-family&colon; -apple-system&comma; BlinkMacSystemFont&comma; 'Segoe UI'&comma; Roboto&comma; Oxygen-Sans&comma; Ubuntu&comma; Cantarell&comma; 'Helvetica Neue'&comma; sans-serif&semi;">n platform versions prior to <&sol;span><span style&equals;"color&colon; &num;008000&semi;"><strong><code dir&equals;"ltr" translate&equals;"no"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;os&sol;Build&period;VERSION&lowbar;CODES&num;Q">Build&period;VERSION&lowbar;CODES&period;Q<&sol;a><&sol;code><&sol;strong><&sol;span><span style&equals;"font-size&colon; 16px&semi; font-family&colon; -apple-system&comma; BlinkMacSystemFont&comma; 'Segoe UI'&comma; Roboto&comma; Oxygen-Sans&comma; Ubuntu&comma; Cantarell&comma; 'Helvetica Neue'&comma; sans-serif&semi;"> <&sol;span><&sol;p> &NewLine;<&sol;li> &NewLine;<li>Followed by either <span style&equals;"color&colon; &num;008000&semi;"><strong>onResume&lpar;&rpar;<&sol;strong><&sol;span> if the activity returns back to the front&comma; or <span style&equals;"color&colon; &num;008000&semi;"><strong>onStop&lpar;&rpar;<&sol;strong><&sol;span> if it becomes invisible to the user&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onStop&lpar;&rpar;<&sol;strong><&sol;span><&sol;h5> &NewLine;<ul> &NewLine;<li class&equals;"p1">Called when the activity is no longer visible to the user&period;<&sol;li> &NewLine;<li class&equals;"p1">This may happen either because a new activity is being started on top&comma; an existing one is being brought in front of this one&comma; or this one is being destroyed&period;<&sol;li> &NewLine;<li>This is a good place to stop refreshing UI&comma; running animations&comma; and other visual things&period;<&sol;li> &NewLine;<li class&equals;"p1">Followed by either <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">onRestart&lpar;&rpar;<&sol;span><&sol;strong><&sol;span> if this activity is coming back to interact with the user&comma; or <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">onDestroy&lpar;&rpar;<&sol;span><&sol;strong><&sol;span> if this activity is going away&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>onDestroy&lpar;&rpar;<&sol;strong><&sol;span><&sol;h5> &NewLine;<ul> &NewLine;<li class&equals;"p1">called before the activity is destroyed&period;<&sol;li> &NewLine;<li class&equals;"p1"> &NewLine;<p>The system invokes this callback either because&colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<ol> &NewLine;<li>the activity is finishing &lpar;due to the user completely dismissing the activity or due to <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;app&sol;Activity&num;finish&lpar;&rpar;">finish&lpar;&rpar;<&sol;a><&sol;strong><&sol;span> being called on the activity&rpar;&comma; or<&sol;li> &NewLine;<li>the system is temporarily destroying the activity due to a configuration change &lpar;such as device rotation or multi-window mode&rpar;<&sol;li> &NewLine;<&sol;ol> &NewLine;<&sol;li> &NewLine;<li> &NewLine;<p>This is where the lifecycle components can clean up anything it needs to before the Activity is destroyed&period;<&sol;p> &NewLine;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h3 class&equals;"graf--h3"><span style&equals;"color&colon; &num;000080&semi;"><strong>Activity Lifecycle Use Cases <&sol;strong><&sol;span><&sol;h3> &NewLine;<p class&equals;"graf--p">Now&comma; let&&num;8217&semi;s see real-life use-cases to understand the lifecycle of an activity&period;<&sol;p> &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><b class&equals;"graf--bold">UseCase 1&colon; <&sol;b><strong>Open an Activity and press the Back button<&sol;strong><&sol;span><&sol;h5> &NewLine;<p class&equals;"graf--p">When you <strong>first time opens<&sol;strong> an app or activity&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>onCreate<&sol;strong><&sol;span>&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">onStart&comma;<&sol;span> <&sol;strong>and <strong><span style&equals;"color&colon; &num;008000&semi;">onResume<&sol;span> <&sol;strong>callbacks will be triggered&period; <&sol;p> &NewLine;<pre class&equals;"graf--p"><code class&equals;"language-java graf graf--code hljs">onCreate -&gt&semi; onStart -&gt&semi; onResume<&sol;code><&sol;pre> &NewLine;<p class&equals;"graf--p">Now <span style&equals;"color&colon; &num;008000&semi;"><strong>onPause<&sol;strong><&sol;span>&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">onStop<&sol;span> <&sol;strong>and <strong><span style&equals;"color&colon; &num;008000&semi;">onDestroy<&sol;span> <&sol;strong>callbacks have triggered&comma; as soon as we <span style&equals;"color&colon; &num;008000&semi;"><strong>press the <span style&equals;"color&colon; &num;0000ff&semi;">back<&sol;span><&sol;strong><&sol;span> button&period;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onPause -&gt&semi; onStop -&gt&semi; onDestroy <&sol;code><&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>UseCase 2&colon; Open an activity and press the Home button<&sol;strong><&sol;span><&sol;h5> &NewLine;<p class&equals;"p1">If we press the <span style&equals;"color&colon; &num;008000&semi;"><b>Home button<&sol;b><&sol;span>&comma; our app will be minimized&period; But it will not be killed&period; So rather than <span style&equals;"color&colon; &num;008000&semi;"><strong>onDestroy<&sol;strong><&sol;span> method call&comma; <b><span style&equals;"color&colon; &num;008000&semi;">onPause<&sol;span> <&sol;b>and <b><span style&equals;"color&colon; &num;008000&semi;">onStop<&sol;span> <&sol;b>will be called&period;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onPause -&gt&semi; onStop <&sol;code><&sol;pre> &NewLine;<p>Now If we reopen the activity again&comma;  <strong><span style&equals;"color&colon; &num;008000&semi;">onRestart<&sol;span><&sol;strong>&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">onStart<&sol;span><&sol;strong>&comma; and <strong><span style&equals;"color&colon; &num;008000&semi;">onResume<&sol;span><&sol;strong> callbacks will be triggered&period;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onRestart -&gt&semi;onStart -&gt&semi; onResume <&sol;code><&sol;pre> &NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>UseCase 3&colon; Open an Activity and Lock the Screen<&sol;strong><&sol;span><&sol;h5> &NewLine;<p class&equals;"p1">If we simply turn off the screen &lpar;press the power button&rpar; while using the app&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>onPause<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;008000&semi;"><strong>onStop<&sol;strong><&sol;span> methods are called&period; <&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onPause -&gt&semi; onStop <&sol;code><&sol;pre> &NewLine;<p class&equals;"p1">If we unlock the screen&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>onRestart<&sol;strong><&sol;span>&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>onStart<&sol;strong><&sol;span>&comma; and <strong><span style&equals;"color&colon; &num;008000&semi;">onResume<&sol;span><&sol;strong> callbacks will be triggered&period;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs"><&sol;code><code class&equals;"language-java graf graf--code hljs">onRestart -&gt&semi;onStart -&gt&semi; onResume <&sol;code><&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>UseCase 4&colon; Open an Activity and Phone Ringing<&sol;strong><&sol;span><&sol;h5> &NewLine;<p class&equals;"p1">Now if an incoming call comes while running activity and if we receive that call&comma; in this case&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>onPause<&sol;strong><&sol;span> and <strong><span style&equals;"color&colon; &num;008000&semi;">onStop<&sol;span><&sol;strong> callbacks will be triggered&period;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onPause -&gt&semi; onStop <&sol;code><&sol;pre> &NewLine;<p class&equals;"p1">If we disconnect the call&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>onRestart<&sol;strong><&sol;span>&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>onStart<&sol;strong><&sol;span>&comma; and <strong><span style&equals;"color&colon; &num;008000&semi;">onResume<&sol;span><&sol;strong> callbacks will be triggered&period;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onRestart -&gt&semi; onStart -&gt&semi; onResume <&sol;code><&sol;pre> &NewLine;<h5 class&equals;"graf--p"><span style&equals;"color&colon; &num;0000ff&semi;"><b class&equals;"graf--bold">UseCase 5&colon; Kill the app from the recent app&&num;8217&semi;s tray<&sol;b><&sol;span><&sol;h5> &NewLine;<p class&equals;"graf--p">When we kill the app from the recent app&&num;8217&semi;s tray&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>onPause<&sol;strong><&sol;span>&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>onStop <&sol;strong><&sol;span>and <span style&equals;"color&colon; &num;008000&semi;"><strong>onDestroy <&sol;strong><&sol;span>callbacks will be triggered&period; <&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onPause -&gt&semi; onStop -&gt&semi; onDestroy <&sol;code><&sol;pre> &NewLine;<p class&equals;"graf--p">Here <strong><span style&equals;"color&colon; &num;008000&semi;">onDestroy<&sol;span><&sol;strong> will kill the instance of the activity&period; So now when we reopen the activity&comma; it will call <span style&equals;"color&colon; &num;008000&semi;"><strong>onCreate<&sol;strong><&sol;span> and not <strong><span style&equals;"color&colon; &num;008000&semi;">onRestart<&sol;span><&sol;strong> to start the activity&period;<&sol;p> &NewLine;<h5 class&equals;"graf--p"><span style&equals;"color&colon; &num;0000ff&semi;"><b class&equals;"graf--bold">UseCase 6&colon; Move from one activity to another<&sol;b><&sol;span><&sol;h5> &NewLine;<p class&equals;"graf--p">In this use case&comma; we will see what happens when we move from one activity to another <b class&equals;"graf--bold">&lpar;<&sol;b>let&&num;8217&semi;s say <span style&equals;"color&colon; &num;008000&semi;"><strong>Activity A to Activity B<&sol;strong><&sol;span><i class&equals;"graf--italic">&rpar;<&sol;i>&period; <&sol;p> &NewLine;<p class&equals;"graf--p"><span class&equals;"graf--italic">When <&sol;span><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Activity A<&sol;strong><&sol;span> will open&comma; the following states are called initially&comma;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onCreate -&gt&semi; onStart -&gt&semi; onResume<&sol;code><&sol;pre> &NewLine;<p class&equals;"graf--p">Let&&num;8217&semi;s say on click of a button we opened <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Activity B<&sol;strong><&sol;span>&period; While opening <strong><span style&equals;"color&colon; &num;0000ff&semi;">Activity B<&sol;span><&sol;strong>&comma; first&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">onPause<&sol;span><&sol;strong> will be called for <strong><span style&equals;"color&colon; &num;0000ff&semi;">Activity A<&sol;span><&sol;strong> and then&comma;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onCreate -&gt&semi; onStart -&gt&semi; onResume<&sol;code><&sol;pre> &NewLine;<p class&equals;"graf--p">will be called for <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Activity B<&sol;strong><&sol;span> and then finally <strong><span style&equals;"color&colon; &num;008000&semi;">onStop<&sol;span><&sol;strong> of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Activity A<&sol;strong><&sol;span> will be called&period;<&sol;p> &NewLine;<p class&equals;"graf--p">Now&comma; when we press the back button from <strong><span style&equals;"color&colon; &num;0000ff&semi;"><i class&equals;"graf--italic">Activity B<&sol;i> <&sol;span><&sol;strong>to <strong><span style&equals;"color&colon; &num;0000ff&semi;">Activity A<&sol;span><&sol;strong>&comma; then first&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">onPause<&sol;span><&sol;strong> of <strong><span style&equals;"color&colon; &num;0000ff&semi;"><i class&equals;"graf--italic">Activity B<&sol;i><&sol;span><&sol;strong> is called and then&comma;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onRestart -&gt&semi; onStart -&gt&semi; onResume<&sol;code><&sol;pre> &NewLine;<p class&equals;"graf--p">gets called for <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Activity A<&sol;strong><&sol;span> and it is displayed to the user&period; Here you can see <strong><span style&equals;"color&colon; &num;008000&semi;">onRestart<&sol;span><&sol;strong> gets called rather than <span style&equals;"color&colon; &num;008000&semi;"><strong>onCreate<&sol;strong><&sol;span> as it is restarting the activity and not creating it&period;<&sol;p> &NewLine;<p class&equals;"graf--p">Then after <strong><span style&equals;"color&colon; &num;008000&semi;">onResume<&sol;span><&sol;strong> of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Activity A<&sol;strong><&sol;span> is called&comma; the following methods of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Activity B<&sol;strong><&sol;span> are called and now <strong><span style&equals;"color&colon; &num;0000ff&semi;">Activity B<&sol;span><&sol;strong> is destroyed &lpar;removed from activity stack&rpar;&period;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onStop -&gt&semi; onDestroy <&sol;code><&sol;pre> &NewLine;<h5 class&equals;"graf--p"><span style&equals;"color&colon; &num;0000ff&semi;"><b class&equals;"graf--bold">UseCase 7&colon; Activity&&num;8217&semi;s  orientation change<&sol;b><&sol;span><&sol;h5> &NewLine;<p>When we change the orientation of an Activity while using the app&comma; the following methods get called&period;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onPause -&gt&semi; onStop -&gt&semi; onDestroy <&sol;code><&sol;pre> &NewLine;<p>After <span style&equals;"color&colon; &num;008000&semi;"><strong>onDestroy<&sol;strong><&sol;span>&comma; the instance of an Activity gets destroyed and a new instance of activity gets created by calling the following methods&period;<&sol;p> &NewLine;<pre><code class&equals;"language-java graf graf--code hljs">onCreate -&gt&semi; onStart -&gt&semi; onResume<&sol;code><&sol;pre> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;&NewLine;

Exit mobile version