{"id":530,"date":"2016-09-22T12:35:10","date_gmt":"2016-09-22T05:35:10","guid":{"rendered":"http:\/\/www.lixsu.xyz\/?p=530"},"modified":"2016-09-22T12:35:10","modified_gmt":"2016-09-22T05:35:10","slug":"android-activity-lifecycle","status":"publish","type":"post","link":"https:\/\/lixionary.com\/index.php\/2016\/09\/22\/android-activity-lifecycle\/","title":{"rendered":"Android Activity Lifecycle"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Willing to develop your own apps? Here i will bring you some basics about it. For me, the most basic thing before doing an android apps is\u00a0knowing how the screen can be populated with fancy UI like in whatsapp, facebook, etc and destroy it.<\/p>\n<p>Android will do couple of actions from you pressed your apps icon till it&#8217;s\u00a0closed again by\u00a0pressing\u00a0back or home. In android apps,\u00a0screen content will be managed by Activity class or its subclass like AppCompatActivity. Android Application can be consisted of more than one activity. Each activity will isolate its state and data from others except you shared them in Application Context. Everything inside application context can be shared between activities in same context (but there&#8217;s a better way to do this by using intent).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-533\" src=\"https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/9f126-screenshot_20160922-115531.png?w=169&#038;h=300\" alt=\"screenshot_20160922-115531\" width=\"169\" height=\"300\" srcset=\"https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/9f126-screenshot_20160922-115531.png 1440w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/9f126-screenshot_20160922-115531-169x300.png 169w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/9f126-screenshot_20160922-115531-576x1024.png 576w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/9f126-screenshot_20160922-115531-768x1365.png 768w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/9f126-screenshot_20160922-115531-864x1536.png 864w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/9f126-screenshot_20160922-115531-1152x2048.png 1152w\" sizes=\"auto, (max-width: 169px) 100vw, 169px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-534\" src=\"https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/84eb7-screenshot_20160922-115535.png?w=169&#038;h=300\" alt=\"screenshot_20160922-115535\" width=\"169\" height=\"300\" srcset=\"https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/84eb7-screenshot_20160922-115535.png 1440w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/84eb7-screenshot_20160922-115535-169x300.png 169w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/84eb7-screenshot_20160922-115535-576x1024.png 576w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/84eb7-screenshot_20160922-115535-768x1365.png 768w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/84eb7-screenshot_20160922-115535-864x1536.png 864w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/84eb7-screenshot_20160922-115535-1152x2048.png 1152w\" sizes=\"auto, (max-width: 169px) 100vw, 169px\" \/><\/p>\n<p>As described above, everything that shown in user&#8217;s screen will depends on activity class, so let&#8217;s figure it out how android manage the UI from activity. The one activity&#8217;s basic is to know its lifecycle. Activity\u00a0entered its life the same time it need to shown to the screen and destroyed when it removed from screen. As described <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html\">here<\/a>, activities will have at least 4 essential state, but here i will provide you more than it because without fully knowing it&#8217;s state, I doubt that you can create a stable apps that give a good UX. Basic flow of android lifecycle can be seen in figure below.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-531 alignleft\" src=\"https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/a761d-activity_lifecycle.png\" alt=\"activity_lifecycle\" width=\"316\" height=\"408\" srcset=\"https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/a761d-activity_lifecycle.png 513w, https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/a761d-activity_lifecycle-232x300.png 232w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/p>\n<ol>\n<li>onCreate()<\/li>\n<li>onStart()<\/li>\n<li>onResume()<\/li>\n<li>onPause()<\/li>\n<li>onStop()<\/li>\n<li>onDestroy()<\/li>\n<li>onRestart()<\/li>\n<li>onSaveInstanceState()<\/li>\n<li>onRestoreInstanceState()<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Then, how to trace back the state of our activity? The easiest way might be by use Log inside every state callback inside your activity like :<\/p>\n<pre><code class=\"java\">\n    @Override\n    protected void onRestart() {\n        Log.i(TAG, \"entering on restart\");\n        super.onRestart();\n    }\n\n    @Override\n    protected void onStart() {\n        Log.i(TAG, \"entering on start\");\n        super.onStart();\n    }\n<\/code><\/pre>\n<p>Here i provide you with 4 scenarios :<\/p>\n<ul>\n<li>app open, app closed<\/li>\n<li>app open, app minimized, app open, app closed<\/li>\n<li>app open, app killed (in low ram then an interrupting task come such as phone call), app open, app closed<\/li>\n<li>app open, app minimized, app killed due to low memory, app open, app closed<\/li>\n<\/ul>\n<p><strong>Scenario #1 (app open, app closed)<\/strong><br \/>\nThis supposed to be most positive case happened, the result is straightforward as figure above<\/p>\n<pre><code class=\"java\">\n09-22 08:19:56.461 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on create\n09-22 08:19:56.601 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on start\n09-22 08:19:56.601 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on resume\n\n#EXIT APPS\n09-22 08:20:15.241 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on pause\n09-22 08:20:15.821 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on stop\n09-22 08:20:15.821 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on destroy\n<\/code><\/pre>\n<p><strong>Scenario #2 (app open, app minimized, app open, app closed)<\/strong><br \/>\nQuite similar with scenario #1, but when we pressed home button, onPause followed by onSaveInstanceState and onStop. onSaveInstanceState will give you chance to store any state, variable of your activity. But later when it&#8217;s called again, it will enter activity from onRestart which is not called savedInstance variable. So no need to worry for this scenario that your activity data lost. (We will discuss it in Scenario #3)<\/p>\n<pre><code>\n09-22 08:21:21.851 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on create\n09-22 08:21:21.871 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on start\n09-22 08:21:21.871 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on resume\n\n# HOME BUTTON PRESSED\n09-22 08:21:30.581 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on pause\n09-22 08:21:31.141 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on save instance state\n09-22 08:21:31.151 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on stop\n\n# BACK INTO APPS\n09-22 08:21:36.541 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on restart\n09-22 08:21:36.551 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on start\n09-22 08:21:36.551 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on resume\n\n# HOME BUTTON PRESSED\n09-22 08:21:41.281 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on pause\n09-22 08:21:41.821 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on save instance state\n09-22 08:21:41.821 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on stop\n\n# BACK INTO APPS\n09-22 08:21:48.801 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on restart\n09-22 08:21:48.801 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on start\n09-22 08:21:48.801 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on resume\n\n# EXIT APPS\n09-22 08:21:50.471 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on pause\n09-22 08:21:50.991 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on stop\n09-22 08:21:50.991 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on destroy\n<\/code><\/pre>\n<p><strong>Scenario #3 (app open, app killed (in low ram then an interrupting task come such as phone call), app open, app closed)<\/strong><br \/>\nOpen -&gt; let task manager kill it in background<br \/>\nSimilar with Scenario #2, but if it killed in background, then when we enter the activities again, it will start from onCreate which will contain savedInstance data inside it and you may restore activity&#8217;s state from it<\/p>\n<pre><code>\n09-22 08:27:31.361 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on create\n09-22 08:27:31.391 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on start\n09-22 08:27:31.391 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on resume\n\n# HOME BUTTON PRESSED\n09-22 08:27:41.481 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on pause\n09-22 08:27:42.021 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on save instance state\n09-22 08:27:42.021 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on stop\n\n# SOMEHOW SYSTEM KILLS IT\n# BACK INTO APPS\n09-22 08:27:55.221 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on create\n09-22 08:27:55.351 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on start\n09-22 08:27:55.361 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on resume\n\n# EXIT APPS\n09-22 08:21:50.471 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on pause\n09-22 08:21:50.991 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on stop\n09-22 08:21:50.991 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on destroy\n<\/code><\/pre>\n<p><strong>Scenario #4 (app open, app minimized, app killed due to low memory, app open, app closed)<\/strong><br \/>\nOpen -&gt; somehow task manager kill it in front of your eyes (it might happened on orientation change event)<br \/>\nThis one quite different with others, instead go through onCreate, it will enter onRestoreInstanceState too. Those callback have savedInstance in it and you can restore Activity&#8217;s state using them. Usually onCreate will only use saveInstance for your main data, and onRestoreInstanceState will contain user input data in field etc so they won&#8217;t lose them. But those depend on your design.<\/p>\n<pre><code>\n09-22 08:27:31.361 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on create\n09-22 08:27:31.391 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on start\n09-22 08:27:31.391 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on resume\n\n# SOMEHOW SYSTEM KILLS IT AND RESTART APPS\n09-22 08:28:26.211 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on pause\n09-22 08:28:26.211 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on save instance state\n09-22 08:28:26.221 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on stop\n09-22 08:28:26.221 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on destroy\n\n09-22 08:28:26.231 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on create\n09-22 08:28:26.261 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on start\n09-22 08:28:26.271 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on restore instance state\n09-22 08:28:26.271 11545-11545\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on resume\n\n# EXIT APPS\n09-22 08:21:50.471 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on pause\n09-22 08:21:50.991 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on stop\n09-22 08:21:50.991 1149-1149\/com.felixsu.saa I\/com.felixsu.saa.MainActivity: entering on destroy\n<\/code><\/pre>\n<p>To check it by yourself, you may install the apps <a href=\"https:\/\/bitbucket.org\/Felixsu\/activity-lifecycle\">here<\/a>, then watch the logcat. To make life easier, set Log Level to INFO and put a filter such as activity name or fragment name into logcat<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-537 size-large\" src=\"http:\/\/www.lixionary.com\/wp-content\/uploads\/2016\/09\/Screen-Shot-2016-09-22-at-12.18.16-1024x275.png\" alt=\"android-studio-log-section\" width=\"1024\" height=\"275\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Willing to develop your own apps? Here i will bring you some basics about it. For me, the most basic thing before doing an android apps is\u00a0knowing how the screen can be populated with fancy UI like in whatsapp, facebook, etc and destroy it. Android will do couple of actions from you pressed your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":536,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[9,7],"tags":[25,39,47,48,263,306],"class_list":["post-530","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge","category-story","tag-activities","tag-android","tag-application","tag-apps","tag-lifecycle","tag-mobile"],"uagb_featured_image_src":{"full":["https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/c0826-android_7_nougat-e1475373379696.jpg",427,240,false],"thumbnail":["https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/c0826-android_7_nougat-e1475373379696-150x150.jpg",150,150,true],"medium":["https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/c0826-android_7_nougat-e1475373379696-300x169.jpg",300,169,true],"medium_large":["https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/c0826-android_7_nougat-e1475373379696.jpg",427,240,false],"large":["https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/c0826-android_7_nougat-e1475373379696.jpg",427,240,false],"1536x1536":["https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/c0826-android_7_nougat-e1475373379696.jpg",427,240,false],"2048x2048":["https:\/\/lixionary.com\/wp-content\/uploads\/2016\/09\/c0826-android_7_nougat-e1475373379696.jpg",427,240,false]},"uagb_author_info":{"display_name":"lixsu92","author_link":"https:\/\/lixionary.com\/index.php\/author\/lixsu92\/"},"uagb_comment_info":0,"uagb_excerpt":"&nbsp; Willing to develop your own apps? Here i will bring you some basics about it. For me, the most basic thing before doing an android apps is\u00a0knowing how the screen can be populated with fancy UI like in whatsapp, facebook, etc and destroy it. Android will do couple of actions from you pressed your&hellip;","_links":{"self":[{"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/comments?post=530"}],"version-history":[{"count":0,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/posts\/530\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/media\/536"}],"wp:attachment":[{"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/media?parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/categories?post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lixionary.com\/index.php\/wp-json\/wp\/v2\/tags?post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}