{"id":15430,"date":"2021-10-05T11:46:10","date_gmt":"2021-10-05T11:46:10","guid":{"rendered":"https:\/\/flobiz.in\/blog\/?p=15430"},"modified":"2021-10-05T11:46:11","modified_gmt":"2021-10-05T11:46:11","slug":"understanding-the-fragment-lifecycle","status":"publish","type":"post","link":"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/","title":{"rendered":"Understanding the Fragment LifeCycle"},"content":{"rendered":"\n<p id=\"df9f\">If you have experimented with making an app, chances are you have probably used Fragments. But have you heard about the&nbsp;<strong><em>Fragment LifeCycle<\/em><\/strong>?<\/p>\n\n\n\n<p id=\"92d1\">It looks something like this :<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1400\/1*dErAx3BMTvVu-SuvjN6quA.png\" alt=\"\"\/><figcaption>Fragment LifeCycle<\/figcaption><\/figure>\n\n\n\n<p id=\"0868\">If you are thinking \u201c&nbsp;<em>That looks like the Activity LifeCycle!&nbsp;<\/em>\u201d, then yes you are right. Fragments have similar lifecycles to Activities. What\u2019s the difference then? Well, a major one would be :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>In activities we use the&nbsp;<strong>onCreate()<\/strong>&nbsp;Method to inflate the layout and bind views while in case of fragments the layout is inflated in&nbsp;<strong>onCreateView()<\/strong>&nbsp;Method and we bind views in the&nbsp;<strong>onViewCreated()<\/strong>&nbsp;method.<\/p><\/blockquote>\n\n\n\n<p id=\"f402\">Now that we know what the LifeCycle looks like, let\u2019s understand it. So open your fragment and override the above methods like this :<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1400\/1*XMKCnB0YLYkpe03pgS5VMw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"fc4b\">When you open&nbsp;your fragment you will notice these in your Logcat :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Fragment LifeCycle: onCreateView Called!<br>Fragment LifeCycle: onViewCreated Called!<br>Fragment LifeCycle: onStart Called!<br>Fragment LifeCycle: onResume Called!<\/p><\/blockquote>\n\n\n\n<p id=\"d731\">Now if you minimize your app you will notice :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Fragment LifeCycle: onPause Called!<br>Fragment LifeCycle: onStop Called!<\/p><\/blockquote>\n\n\n\n<p id=\"8a3d\">And if you open it again you will notice :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Fragment LifeCycle: onStart Called!<br>Fragment LifeCycle: onResume Called!<\/p><\/blockquote>\n\n\n\n<p id=\"ac23\">That makes things a little clearer, but are&nbsp;<strong><em>onPause()<\/em><\/strong>&nbsp;and&nbsp;<strong><em>onResume()&nbsp;<\/em><\/strong>only called with other methods? No,&nbsp;<strong><em>onPause()<\/em><\/strong>&nbsp;is called when the app loses focus and&nbsp;<strong><em>OnResume()<\/em><\/strong>&nbsp;is called when it is in focus. To test this just add a button to your fragment layout and on its Click Listener write the following code :<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1400\/1*jAvhExaOOmj7BbQNL3Th6g.png\" alt=\"Fragment LifeCycle\"\/><figcaption>Implicit Intent<\/figcaption><\/figure>\n\n\n\n<p id=\"7019\">This is just an implicit intent to send some data to someone else. When you click this button you will notice in your Logcat :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Fragment LifeCycle: onPause Called!<\/p><\/blockquote>\n\n\n\n<p id=\"6b88\">Now if you click outside the dialogue you will notice :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Fragment LifeCycle: onResume Called!<\/p><\/blockquote>\n\n\n\n<p id=\"450d\">But what happens to the&nbsp;<strong>ViewModel&nbsp;<\/strong>during all this? Well let\u2019s experiment with that as well. Add the following methods in your ViewModel :<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1400\/1*xvjtewU3kaCFXr0KEep2Bw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"f2d2\">Now when you start your app you will also notice :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Fragment LifeCycle: viewModel Created!<\/p><\/blockquote>\n\n\n\n<p id=\"fa6e\">Now rotate your mobile or emulator, you will not see the&nbsp;<em>\u201cviewModel Destroyed\u201d&nbsp;<\/em>entry in your Logcat, this is because the ViewModel does not get destroyed on configuration changes and thus can be used to store data.<\/p>\n\n\n\n<p id=\"6cc5\">But does the ViewModel get destroyed if we navigate to another fragment and what about the fragment itself? Let\u2019s try it out!<\/p>\n\n\n\n<p id=\"31db\">Add another button to your layout and in its ClickListener add the following code :<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1400\/1*HXOi7CKjDT79R0KJYyvwtw.png\" alt=\"\"\/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Note : You will need to setup&nbsp;<a href=\"https:\/\/developer.android.com\/guide\/navigation\/navigation-getting-started\" target=\"_blank\" rel=\"noreferrer noopener\">Navigation Component<\/a>&nbsp;to navigate between fragments.<\/p><\/blockquote>\n\n\n\n<p id=\"9c0b\">You will notice the following in your Logcat :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Fragment LifeCycle: onPause Called!<br>Fragment LifeCycle: onStop Called!<br>Fragment LifeCycle: onDestroyView Called!<\/p><\/blockquote>\n\n\n\n<p id=\"84ec\"><strong><em>Note<\/em><\/strong><em>&nbsp;: If you add Debug statements to Fragment2&#8217;s LifeCycle methods, you will notice that Fragment2\u2019s onCreate(), onCreateView(), onViewCreated(), onStart() and onResume() methods are called before the above appears in the Logcat.<\/em><\/p>\n\n\n\n<p id=\"b1ec\">As you can see the ViewModel still does not get Destroyed and if you navigate back to the first fragment using the back button then you will see that any data stored in the ViewModel is preserved and the&nbsp;<strong>onCreate()<\/strong>&nbsp;method is not called as the fragment was never destroyed.<\/p>\n\n\n\n<p id=\"6a43\">When does the ViewModel get destroyed? Close the app and you will notice in the Logcat :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Fragment LifeCycle: onPause Called!<br>Fragment LifeCycle: onStop Called!<br>Fragment LifeCycle: onDestroyView Called!<br>Fragment LifeCycle: viewModel Destroyed!<br>Fragment LifeCycle: onDestroy Called!<\/p><\/blockquote>\n\n\n\n<p id=\"2864\">That\u2019s it. Thanks for reading!<a href=\"https:\/\/medium.com\/m\/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fbookmark%2Fp%2Fc730a3263a6b&amp;operation=register&amp;redirect=https%3A%2F%2Fmedium.com%2F%40pratham_21198%2Funderstanding-the-fragment-lifecycle-c730a3263a6b&amp;source=post_sidebar-----c730a3263a6b---------------------bookmark_sidebar-----------\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fragment LifeCycle looks similar to Activities LifeCycle. But, they aren&#8217;t the same. Read this blog to know the major difference. <\/p>\n","protected":false},"author":11,"featured_media":15432,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[149],"tags":[],"class_list":["post-15430","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-engineering-product"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Understanding the Fragment LifeCycle<\/title>\n<meta name=\"description\" content=\"Fragment LifeCycle looks similar to Activities LifeCycle. But, they aren&#039;t same. Read this blog to know the major difference between them.\" \/>\n<link rel=\"canonical\" href=\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding the Fragment LifeCycle\" \/>\n<meta property=\"og:description\" content=\"Fragment LifeCycle looks similar to Activities LifeCycle. But, they aren&#039;t same. Read this blog to know the major difference between them.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/\" \/>\n<meta property=\"og:site_name\" content=\"FloBiz\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-05T11:46:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-10-05T11:46:11+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/35.200.248.73\/blog\/wp-content\/uploads\/2021\/10\/Fragment-LifeCycle.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"4 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/35.200.248.73\/blog\/#website\",\"url\":\"http:\/\/35.200.248.73\/blog\/\",\"name\":\"FloBiz\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"http:\/\/35.200.248.73\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/35.200.248.73\/blog\/wp-content\/uploads\/2021\/10\/Fragment-LifeCycle.png\",\"contentUrl\":\"http:\/\/35.200.248.73\/blog\/wp-content\/uploads\/2021\/10\/Fragment-LifeCycle.png\",\"width\":1200,\"height\":675,\"caption\":\"Fragment LifeCycle\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/#webpage\",\"url\":\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/\",\"name\":\"Understanding the Fragment LifeCycle\",\"isPartOf\":{\"@id\":\"http:\/\/35.200.248.73\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/#primaryimage\"},\"datePublished\":\"2021-10-05T11:46:10+00:00\",\"dateModified\":\"2021-10-05T11:46:11+00:00\",\"author\":{\"@id\":\"http:\/\/35.200.248.73\/blog\/#\/schema\/person\/eacf6891f6d9b106da3ef107d2e17a60\"},\"description\":\"Fragment LifeCycle looks similar to Activities LifeCycle. But, they aren't same. Read this blog to know the major difference between them.\",\"breadcrumb\":{\"@id\":\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/35.200.248.73\/blog\/\",\"url\":\"https:\/\/35.200.248.73\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/\",\"url\":\"http:\/\/35.200.248.73\/blog\/understanding-the-fragment-lifecycle\/\",\"name\":\"Understanding the Fragment LifeCycle\"}}]},{\"@type\":\"Person\",\"@id\":\"http:\/\/35.200.248.73\/blog\/#\/schema\/person\/eacf6891f6d9b106da3ef107d2e17a60\",\"name\":\"Pratham\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/35.200.248.73\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9b1771c74ece4f5c094a51a9eccb15303838e95674e56868fe366ced33afbcf?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9b1771c74ece4f5c094a51a9eccb15303838e95674e56868fe366ced33afbcf?s=96&d=mm&r=g\",\"caption\":\"Pratham\"},\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/pratham-kumar-jha\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/posts\/15430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/comments?post=15430"}],"version-history":[{"count":3,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/posts\/15430\/revisions"}],"predecessor-version":[{"id":15436,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/posts\/15430\/revisions\/15436"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/media\/15432"}],"wp:attachment":[{"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/media?parent=15430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/categories?post=15430"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/35.200.248.73\/blog\/wp-json\/wp\/v2\/tags?post=15430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}