{"id":255,"date":"2023-05-16T12:41:54","date_gmt":"2023-05-16T12:41:54","guid":{"rendered":"https:\/\/itnotes.apjsoftwares.com\/?p=255"},"modified":"2023-05-16T12:41:54","modified_gmt":"2023-05-16T12:41:54","slug":"1-2-basic-components-of-android-activity-life-cycle","status":"publish","type":"post","link":"https:\/\/itnotes.apjsoftwares.in\/index.php\/2023\/05\/16\/1-2-basic-components-of-android-activity-life-cycle\/","title":{"rendered":"1.2 Basic components of Android, activity life cycle"},"content":{"rendered":"\n<p><strong>Application Components<\/strong><\/p>\n\n\n\n<p>Application components are the essential building blocks of an Android application. These components are loosely coupled by the application manifest file AndroidManifest.xml that describes each component of the application and how they interact.<\/p>\n\n\n\n<p>There are following four main components that can be used within an Android application \u2212<\/p>\n\n\n\n<ul>\n<li><strong>Activities<\/strong><\/li>\n<\/ul>\n\n\n\n<p>An activity represents a single screen with a user interface,in-short Activity performs actions on the screen. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails.&nbsp;<\/p>\n\n\n\n<p>If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched.<\/p>\n\n\n\n<p>An activity is implemented as a subclass of Activity class as follows \u2212<\/p>\n\n\n\n<p>public class MainActivity extends Activity {<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><strong>Services<\/strong><\/p>\n\n\n\n<p>A service is a component that runs in the background to perform long-running operations. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity.<\/p>\n\n\n\n<p>A service is implemented as a subclass of Service class as follows \u2212<\/p>\n\n\n\n<p>public class MyService extends Service {<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><strong>Broadcast Receivers<\/strong><\/p>\n\n\n\n<p>Broadcast Receivers simply respond to broadcast messages from other applications or from the system. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is a broadcast receiver who will intercept this communication and will initiate appropriate action.<\/p>\n\n\n\n<p>A broadcast receiver is implemented as a subclass of Broadcast Receiver class and each message is broadcast as an Intent object.<\/p>\n\n\n\n<p>public class MyReceiver&nbsp; extends&nbsp; BroadcastReceiver {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;public void onReceive(context,intent){}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><strong>Content Providers<\/strong><\/p>\n\n\n\n<p>A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. The data may be stored in the file system, the database or somewhere else entirely.<\/p>\n\n\n\n<p>A content provider is implemented as a subclass of Content Provider class and must implement a standard set of APIs that enable other applications to perform transactions.<\/p>\n\n\n\n<p>public class MyContentProvider extends&nbsp; ContentProvider {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;public void onCreate(){}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><strong>Additional Components<\/strong><\/p>\n\n\n\n<p>There are additional components which will be used in the construction of above mentioned entities, their logic, and wiring between them. These components are \u2212<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>S.No<\/td><td>Components &amp; Description<\/td><\/tr><tr><td>1<\/td><td><strong>Fragments<\/strong>Represents a portion of the user interface in an Activity.<\/td><\/tr><tr><td>2<\/td><td><strong>Views<\/strong>UI elements that are drawn on-screen including buttons, lists forms etc.<\/td><\/tr><tr><td>3<\/td><td><strong>Layouts<\/strong>View hierarchies that control screen format and appearance of the views.<\/td><\/tr><tr><td>4<\/td><td><strong>Intents<\/strong>Messages wiring components together.<\/td><\/tr><tr><td>5<\/td><td><strong>Resources<\/strong>External elements, such as strings, constants and drawable pictures.<\/td><\/tr><tr><td>6<\/td><td><strong>Manifest<\/strong>Configuration file for the application.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Android Activity Lifecycle<\/strong><\/h2>\n\n\n\n<p>Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper class.<\/p>\n\n\n\n<p>An activity is the single screen in android. It is like a window or frame of Java. By the help of activity, you can place all your UI components or widgets in a single screen.<\/p>\n\n\n\n<p>The 7 lifecycle method of Activity describes how activity will behave at different states.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Method<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td><strong>onCreate<\/strong><\/td><td>called when activity is first created.<\/td><\/tr><tr><td><strong>onStart<\/strong><\/td><td>called when activity is becoming visible to the user.<\/td><\/tr><tr><td><strong>onResume<\/strong><\/td><td>called when activity will start interacting with the user.<\/td><\/tr><tr><td><strong>onPause<\/strong><\/td><td>called when activity is not visible to the user.<\/td><\/tr><tr><td><strong>onStop<\/strong><\/td><td>called when activity is no longer visible to the user.<\/td><\/tr><tr><td><strong>onRestart<\/strong><\/td><td>called after your activity is stopped, prior to start.<\/td><\/tr><tr><td><strong>onDestroy<\/strong><\/td><td>called before the activity is destroyed.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"523\" height=\"673\" src=\"https:\/\/itnotes.apjsoftwares.com\/wp-content\/uploads\/2023\/05\/image-53.png\" alt=\"\" class=\"wp-image-256\" srcset=\"https:\/\/itnotes.apjsoftwares.in\/wp-content\/uploads\/2023\/05\/image-53.png 523w, https:\/\/itnotes.apjsoftwares.in\/wp-content\/uploads\/2023\/05\/image-53-233x300.png 233w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/figure>\n\n\n\n<p><strong>Android Activity Lifecycle Example<\/strong><\/p>\n\n\n\n<p>import android.app.Activity;&nbsp;&nbsp;<\/p>\n\n\n\n<p>import android.os.Bundle;&nbsp;&nbsp;<\/p>\n\n\n\n<p>import android.util.Log;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>public class MainActivity extends Activity {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;@Override&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;protected void onCreate(Bundle savedInstanceState) {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onCreate(savedInstanceState);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setContentView(R.layout.activity_main);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(&#8220;lifecycle&#8221;,&#8221;onCreate invoked&#8221;);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;@Override&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;protected void onStart() {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onStart();&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(&#8220;lifecycle&#8221;,&#8221;onStart invoked&#8221;);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;@Override&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;protected void onResume() {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onResume();&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(&#8220;lifecycle&#8221;,&#8221;onResume invoked&#8221;);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;@Override&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;protected void onPause() {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onPause();&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(&#8220;lifecycle&#8221;,&#8221;onPause invoked&#8221;);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;@Override&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;protected void onStop() {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onStop();&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(&#8220;lifecycle&#8221;,&#8221;onStop invoked&#8221;);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;@Override&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;protected void onRestart() {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onRestart();&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(&#8220;lifecycle&#8221;,&#8221;onRestart invoked&#8221;);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;@Override&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;protected void onDestroy() {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onDestroy();&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(&#8220;lifecycle&#8221;,&#8221;onDestroy invoked&#8221;);&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>}&nbsp;&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Application Components Application components are the essential building blocks of an Android application. These components&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/255"}],"collection":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/comments?post=255"}],"version-history":[{"count":1,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":257,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/255\/revisions\/257"}],"wp:attachment":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/media?parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/categories?post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/tags?post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}