{"id":37003,"date":"2024-11-01T09:53:59","date_gmt":"2024-11-01T09:53:59","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37003"},"modified":"2024-11-01T11:42:39","modified_gmt":"2024-11-01T11:42:39","slug":"kotlin-android-app-development-course-activity-lifecycle","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37003\/","title":{"rendered":"Kotlin Android App Development Course, Activity Lifecycle"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this article, we will take a closer look at one of the most important concepts in Android app development, the <strong>Activity Lifecycle<\/strong>. An Activity is a fundamental component that composes a screen where users interact, and its lifecycle defines how it changes as the app transitions between various states. As users use the app, the Activity goes through several states, and we will learn how to save or restore important information during this process.<\/p>\n<h2>What is an Activity?<\/h2>\n<p>An Activity is the most basic component that represents a screen where users can interact in Android. Activities allow for various tasks to be performed through different UI components, and multiple Activities can be combined to build an app. For example, a photo gallery app may have an Activity for selecting photos, an Activity for viewing photos, and a settings Activity.<\/p>\n<h2>Activity Lifecycle<\/h2>\n<p>The lifecycle of an Activity is managed through several callback methods such as <strong>onCreate()<\/strong>, <strong>onStart()<\/strong>, <strong>onResume()<\/strong>, <strong>onPause()<\/strong>, <strong>onStop()<\/strong>, and <strong>onDestroy()<\/strong>. Each method is called when the state of the Activity changes, allowing the developer to control the operation of the Activity as declared in the manifest.<\/p>\n<h3>1. onCreate()<\/h3>\n<p>This method is called when the application is first launched and typically performs UI initialization tasks. It is called only once when the Activity is created for the first time. Here, the layout is set up and data binding is usually performed.<\/p>\n<pre><code class=\"language-kotlin\">\nclass MainActivity : AppCompatActivity() {\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n    }\n}\n    <\/code><\/pre>\n<h3>2. onStart()<\/h3>\n<p>This method is called when the Activity starts to become visible to the user. It is invoked just before the Activity begins to interact with the user, meaning the Activity is on the screen but has not yet gained focus.<\/p>\n<pre><code class=\"language-kotlin\">\noverride fun onStart() {\n    super.onStart()\n    \/\/ Prepare tasks for the Activity to be shown to the user.\n}\n    <\/code><\/pre>\n<h3>3. onResume()<\/h3>\n<p>This method is called when the user can interact with the Activity. You can write code here to optimize the UI and user interactions.<\/p>\n<pre><code class=\"language-kotlin\">\noverride fun onResume() {\n    super.onResume()\n    \/\/ This is called when the Activity gains focus and performs UI update tasks.\n}\n    <\/code><\/pre>\n<h3>4. onPause()<\/h3>\n<p>This method is called when another Activity begins to be shown to the user. It is mainly used for saving data or releasing resources.<\/p>\n<pre><code class=\"language-kotlin\">\noverride fun onPause() {\n    super.onPause()\n    \/\/ Save necessary data before leaving the Activity.\n}\n    <\/code><\/pre>\n<h3>5. onStop()<\/h3>\n<p>This method is called when the Activity is no longer visible. For example, it is called when another Activity completely obscures the screen or when the app moves to the background. Necessary resources can be released here if required.<\/p>\n<pre><code class=\"language-kotlin\">\noverride fun onStop() {\n    super.onStop()\n    \/\/ Stop UI updates and release resources as needed.\n}\n    <\/code><\/pre>\n<h3>6. onDestroy()<\/h3>\n<p>This is the last method called when the Activity is terminated. Necessary tasks for memory cleanup can be performed here, usually when the Activity is no longer in use. It cleans up all resources.<\/p>\n<pre><code class=\"language-kotlin\">\noverride fun onDestroy() {\n    super.onDestroy()\n    \/\/ Perform Activity cleanup and resource release tasks.\n}\n    <\/code><\/pre>\n<h2>Activity Lifecycle Sequence<\/h2>\n<p>The lifecycle states when an Activity is created and terminated can be represented in the following diagram.<\/p>\n<p><img decoding=\"async\" alt=\"Activity Lifecycle\" src=\"activity_lifecycle.png\"\/><\/p>\n<h2>Example of Utilizing Lifecycle Methods<\/h2>\n<p>Now, let\u2019s demonstrate how to use Activity lifecycle methods with a practical example. Below is an example of a simple counter app that increments the count each time a button is clicked in the user interface, demonstrating how to save and restore data using lifecycle methods.<\/p>\n<pre><code class=\"language-kotlin\">\nclass CounterActivity : AppCompatActivity() {\n    private var counter = 0\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_counter)\n        \n        \/\/ Restore previous state\n        savedInstanceState?.let {\n            counter = it.getInt(\"counter\", 0)\n        }\n\n        findViewById<button>(R.id.button_increase).setOnClickListener {\n            counter++\n            updateCounterDisplay()\n        }\n\n        updateCounterDisplay()\n    }\n\n    override fun onSaveInstanceState(outState: Bundle) {\n        super.onSaveInstanceState(outState)\n        \/\/ Save data\n        outState.putInt(\"counter\", counter)\n    }\n\n    private fun updateCounterDisplay() {\n        findViewById<textview>(R.id.text_counter).text = counter.toString()\n    }\n}\n    <\/textview><\/button><\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this article, we explored the Activity lifecycle in Android using Kotlin. Understanding and effectively utilizing the Activity lifecycle can greatly enhance app performance and improve user experience. In the next lesson, we will look at other Android components such as Services and Broadcast Receivers. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this article, we will take a closer look at one of the most important concepts in Android app development, the Activity Lifecycle. An Activity is a fundamental component that composes a screen where users interact, and its lifecycle defines how it changes as the app transitions between various states. As users use the &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37003\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Kotlin Android App Development Course, Activity Lifecycle&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[143],"tags":[],"class_list":["post-37003","post","type-post","status-publish","format-standard","hentry","category-kotlin-android-app-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Kotlin Android App Development Course, Activity Lifecycle - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/37003\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Kotlin Android App Development Course, Activity Lifecycle - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this article, we will take a closer look at one of the most important concepts in Android app development, the Activity Lifecycle. An Activity is a fundamental component that composes a screen where users interact, and its lifecycle defines how it changes as the app transitions between various states. As users use the &hellip; \ub354 \ubcf4\uae30 &quot;Kotlin Android App Development Course, Activity Lifecycle&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37003\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:53:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:42:39+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/37003\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37003\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Kotlin Android App Development Course, Activity Lifecycle\",\"datePublished\":\"2024-11-01T09:53:59+00:00\",\"dateModified\":\"2024-11-01T11:42:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37003\/\"},\"wordCount\":525,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin Android app development\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37003\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37003\/\",\"name\":\"Kotlin Android App Development Course, Activity Lifecycle - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:53:59+00:00\",\"dateModified\":\"2024-11-01T11:42:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37003\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37003\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37003\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Kotlin Android App Development Course, Activity Lifecycle\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Kotlin Android App Development Course, Activity Lifecycle - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/37003\/","og_locale":"ko_KR","og_type":"article","og_title":"Kotlin Android App Development Course, Activity Lifecycle - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this article, we will take a closer look at one of the most important concepts in Android app development, the Activity Lifecycle. An Activity is a fundamental component that composes a screen where users interact, and its lifecycle defines how it changes as the app transitions between various states. As users use the &hellip; \ub354 \ubcf4\uae30 \"Kotlin Android App Development Course, Activity Lifecycle\"","og_url":"https:\/\/atmokpo.com\/w\/37003\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:53:59+00:00","article_modified_time":"2024-11-01T11:42:39+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/37003\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37003\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Kotlin Android App Development Course, Activity Lifecycle","datePublished":"2024-11-01T09:53:59+00:00","dateModified":"2024-11-01T11:42:39+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37003\/"},"wordCount":525,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin Android app development"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37003\/","url":"https:\/\/atmokpo.com\/w\/37003\/","name":"Kotlin Android App Development Course, Activity Lifecycle - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:53:59+00:00","dateModified":"2024-11-01T11:42:39+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37003\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37003\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37003\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Kotlin Android App Development Course, Activity Lifecycle"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=37003"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37003\/revisions"}],"predecessor-version":[{"id":37004,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37003\/revisions\/37004"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}