{"id":56008,"date":"2024-01-03T16:20:27","date_gmt":"2024-01-03T10:50:27","guid":{"rendered":"https:\/\/www.oneclickitsolution.com\/blog\/?p=56008"},"modified":"2025-03-20T10:45:45","modified_gmt":"2025-03-20T05:15:45","slug":"activity-life-cycle-components-for-android","status":"publish","type":"post","link":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android","title":{"rendered":"Custom Activity Life Cycle Components for Android"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>The activity life cycle components provide several callbacks which are used to create, stop, or resume an activity, or destroy the process in which the activity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why is it Important to Develop Mobile Apps?<\/h2>\n\n\n\n<p>In activity lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. For example, we make a streaming video player, you need to stop the video and terminate the network connection when the user switches to another app from the existing app.<\/p>\n\n\n\n<p>The main use is that each callback allows you to perform a specific task that&#8217;s appropriate to a given change of state in the entire application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-prerequisite\">Prerequisite:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Android Device, Windows\/Linux<\/li>\n\n\n\n<li>Tech Stack, We\u2019re using: Kotlin, java, and XML language<\/li>\n\n\n\n<li>Tools we are using: Android studio<\/li>\n\n\n\n<li><a href=\"https:\/\/www.oracle.com\/java\/technologies\/javase-downloads.html\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Java Development Kit<\/a>.&nbsp;<\/li>\n\n\n\n<li>Android Studio 3.0 or later.<\/li>\n\n\n\n<li>Android SDK API Level 16 or higher.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.oneclickitsolution.com\/contact-us\/\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"300\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/04\/Hire-Dedicated-Developer.png\" alt=\"Hire Dedicated Developer\" class=\"wp-image-54641\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/04\/Hire-Dedicated-Developer.png 1200w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/04\/Hire-Dedicated-Developer-768x192.png 768w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/04\/Hire-Dedicated-Developer-20x5.png 20w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Process of Implementing Activity Life Cycle in Android<\/h2>\n\n\n\n<p><strong>Activity class provides a core set of six callbacks:<\/strong> <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity#onCreate(android.os.Bundle)\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">onCreate()<\/a>, <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity#onStart()\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">onStart()<\/a>, <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity#onResume()\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">onResume()<\/a>, <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity#onPause()\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">onPause()<\/a>, <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity#onStop()\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">onStop()<\/a>, and <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity#onDestroy()\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">onDestroy()<\/a>&nbsp; this is most important for developed.<\/p>\n\n\n\n<p>The below Screenshot shows how the activity lifecycle actually works<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"513\" height=\"663\" src=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/how-the-activity-lifecycle-actually-works.png\" alt=\"how the activity lifecycle in android actually works \" class=\"wp-image-56013\" srcset=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/how-the-activity-lifecycle-actually-works.png 513w, https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/how-the-activity-lifecycle-actually-works-15x20.png 15w\" sizes=\"(max-width: 513px) 100vw, 513px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"markup\" class=\"language-markup\">OnCreate() Called when the activity is first created.\nOnStart() Called when the activity becomes visible to the user.\nOnResume() Called when the activity starts interacting with the user.\nOnPause() Called when the current activity is being paused and the previous activity is resumed.\nOnStop() Called when the activity is no longer visible to the user.\nOnDestroy() Called before the activity is destroyed by the system(either manually or by the system to conserve memory)\nOnRestart() Called when the activity has been stopped and is restarting again.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Implementing Custom Lifecycle Components<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"markup\" class=\"language-markup\">class MyActivity : Activity(), LifecycleOwner {\n    private lateinit var lifecycleRegistry: LifecycleRegistry\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        lifecycleRegistry = LifecycleRegistry(this)\n        lifecycleRegistry.markState(Lifecycle.State.CREATED)\n    }\n    public override fun onStart() {\n        super.onStart()\n        lifecycleRegistry.markState(Lifecycle.State.STARTED)\n    }\n    override fun getLifecycle(): Lifecycle {\n        return lifecycleRegistry\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why is the customization of life cycle components important?<\/h2>\n\n\n\n<p>Customization of the activity life cycle components is important for every activity and fragments are now subclass LifeCycleObserver, you can get an instance of their Lifecycle with the call function and give that Lifecycle instance to the LifecycleObserver class. That is the way you set up a lifecycle observation. <a href=\"https:\/\/www.oneclickitsolution.com\/hire-dedicated-kotlin-developers\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Kotlin<\/strong> <\/a>offers a modern and powerful language alternative for Android app development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>I would like to conclude this blog is used for access when using any media player rather than switching between any activity\/fragment to another activity\/fragment.<\/p>\n\n\n\n<p>There is no guarantee that <a href=\"https:\/\/www.oneclickitsolution.com\/services\/android-app-development\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>app components<\/strong><\/a> start before the activity\/fragment stop. this long running operation so some configuration checks in onStart().This can cause a onStop() method finishes before onStart().<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1664792860060\"><strong class=\"schema-faq-question\">Can I make a custom UI?<\/strong> <p class=\"schema-faq-answer\">Yes, you can make it.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1664792866506\"><strong class=\"schema-faq-question\">Why do media or youtube players play when switching any activity\/fragment?<\/strong> <p class=\"schema-faq-answer\">You can use this custom life cycle component to fix this issue.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction The activity life cycle components provide several callbacks which are used to create, stop, or resume an activity, or destroy the process in which the activity. Why is it Important to Develop Mobile Apps? In activity lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. &hellip;<\/p>\n","protected":false},"author":73,"featured_media":56010,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[516,838],"tags":[901,795],"class_list":["post-56008","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile-apps","category-solutions","tag-android","tag-mobile-app-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v18.2.1 (Yoast SEO v24.8.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Custom Activity Life Cycle Components for Android Apps<\/title>\n<meta name=\"description\" content=\"The custom activity life cycle components for android are used to create, stop, resume, or destroy the process in which the activity.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom Activity Life Cycle Components for Android Apps\" \/>\n<meta property=\"og:description\" content=\"The custom activity life cycle components for android are used to create, stop, resume, or destroy the process in which the activity.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android\" \/>\n<meta property=\"og:site_name\" content=\"OneClick IT Consultancy\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/oneclickconsultancy\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-03T10:50:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-20T05:15:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/custom-activity-life-cycle-components-for-android.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Jigar Viradiya\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@OneclickIT\" \/>\n<meta name=\"twitter:site\" content=\"@OneclickIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jigar Viradiya\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Custom Activity Life Cycle Components for Android Apps","description":"The custom activity life cycle components for android are used to create, stop, resume, or destroy the process in which the activity.","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:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android","og_locale":"en_US","og_type":"article","og_title":"Custom Activity Life Cycle Components for Android Apps","og_description":"The custom activity life cycle components for android are used to create, stop, resume, or destroy the process in which the activity.","og_url":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android","og_site_name":"OneClick IT Consultancy","article_publisher":"https:\/\/www.facebook.com\/oneclickconsultancy","article_published_time":"2024-01-03T10:50:27+00:00","article_modified_time":"2025-03-20T05:15:45+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/custom-activity-life-cycle-components-for-android.png","type":"image\/png"}],"author":"Jigar Viradiya","twitter_card":"summary_large_image","twitter_creator":"@OneclickIT","twitter_site":"@OneclickIT","twitter_misc":{"Written by":"Jigar Viradiya","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#article","isPartOf":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android"},"author":{"name":"Jigar Viradiya","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/person\/63dc606d4c5e594a3fdeab9994adf2b6"},"headline":"Custom Activity Life Cycle Components for Android","datePublished":"2024-01-03T10:50:27+00:00","dateModified":"2025-03-20T05:15:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android"},"wordCount":365,"publisher":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#primaryimage"},"thumbnailUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/custom-activity-life-cycle-components-for-android.png","keywords":["Android","Mobile App Development"],"articleSection":["Mobile Application","Solutions"],"inLanguage":"en-US"},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android","url":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android","name":"Custom Activity Life Cycle Components for Android Apps","isPartOf":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#primaryimage"},"image":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#primaryimage"},"thumbnailUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/custom-activity-life-cycle-components-for-android.png","datePublished":"2024-01-03T10:50:27+00:00","dateModified":"2025-03-20T05:15:45+00:00","description":"The custom activity life cycle components for android are used to create, stop, resume, or destroy the process in which the activity.","breadcrumb":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#faq-question-1664792860060"},{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#faq-question-1664792866506"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#primaryimage","url":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/custom-activity-life-cycle-components-for-android.png","contentUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/custom-activity-life-cycle-components-for-android.png","width":1200,"height":628,"caption":"custom activity life cycle components for android"},{"@type":"BreadcrumbList","@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.oneclickitsolution.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Custom Activity Life Cycle Components for Android"}]},{"@type":"WebSite","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#website","url":"https:\/\/www.oneclickitsolution.com\/blog\/","name":"OneClick IT Consultancy","description":"We Build Brands from Ideas","publisher":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#organization"},"alternateName":"OneClick IT Solution","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.oneclickitsolution.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#organization","name":"OneClick IT Consultancy","alternateName":"OneClick IT Solution","url":"https:\/\/www.oneclickitsolution.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/oneclick-official-logo.png","contentUrl":"https:\/\/www.oneclickitsolution.com\/blog\/wp-content\/uploads\/2022\/10\/oneclick-official-logo.png","width":100,"height":100,"caption":"OneClick IT Consultancy"},"image":{"@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/oneclickconsultancy","https:\/\/x.com\/OneclickIT","https:\/\/www.instagram.com\/oneclick.it.consultancy\/","https:\/\/www.linkedin.com\/company\/one-click-it-consultancy\/","https:\/\/www.pinterest.com\/oneclickitconsultancy\/","https:\/\/www.youtube.com\/channel\/UCsEG6aiwOwvYrcZxMoP5xjg","https:\/\/oneclickit.tumblr.com\/","https:\/\/dribbble.com\/oneclickitconsultancy"]},{"@type":"Person","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/person\/63dc606d4c5e594a3fdeab9994adf2b6","name":"Jigar Viradiya","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.oneclickitsolution.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6c255f25a6583c5bd7d312ba36abb3f4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6c255f25a6583c5bd7d312ba36abb3f4?s=96&d=mm&r=g","caption":"Jigar Viradiya"},"description":"I am currently doing job as an Android developer in OneClick IT Consultancy past one year, I am very good in solving various types of problems to help our clients.","sameAs":["https:\/\/www.oneclickitsolution.com\/blog\/"],"jobTitle":"Software Engineer","url":"https:\/\/www.oneclickitsolution.com\/blog\/author\/jigar-viradiya"},{"@type":"Question","@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#faq-question-1664792860060","position":1,"url":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#faq-question-1664792860060","name":"Can I make a custom UI?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Yes, you can make it.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#faq-question-1664792866506","position":2,"url":"https:\/\/www.oneclickitsolution.com\/blog\/activity-life-cycle-components-for-android#faq-question-1664792866506","name":"Why do media or youtube players play when switching any activity\/fragment?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"You can use this custom life cycle component to fix this issue.","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts\/56008"}],"collection":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/users\/73"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/comments?post=56008"}],"version-history":[{"count":1,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts\/56008\/revisions"}],"predecessor-version":[{"id":63186,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/posts\/56008\/revisions\/63186"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/media\/56010"}],"wp:attachment":[{"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/media?parent=56008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/categories?post=56008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oneclickitsolution.com\/blog\/wp-json\/wp\/v2\/tags?post=56008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}