{"id":4888,"date":"2019-06-21T13:52:26","date_gmt":"2019-06-21T08:22:26","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4888"},"modified":"2019-06-21T13:52:33","modified_gmt":"2019-06-21T08:22:33","slug":"fundamental-things-to-know-in-android-activity-and-fragment","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/fundamental-things-to-know-in-android-activity-and-fragment\/","title":{"rendered":"Fundamental things to know in android activity and fragment"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Fundamental things to know in android activity and fragment<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Date : 12\/06\/2019<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">Activity<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"> The <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html\" target=\"_blank\" rel=\"noopener\"><strong><code>Activity<\/code><\/strong><\/a>class is basically a crucial component of an Android app, and the way activities are launched and put together is a fundamental part of the platform&#8217;s application model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Hence an activity provides the window in which the app draws its UI.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Actually an XML file and Activity for implementation works together to display a single screen in a app.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Most apps contain multiple screens, which means they comprise multiple activities.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> <strong>Each activity can then start another activity in order to perform different actions. To do this we have to INTENT.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> For your app to be able to use activities, basically you must declare the activities, and certain of their attributes, in the manifest.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;manifest&gt;\n   &lt;application&gt;\n     &lt;activity android:name=\".ExampleActivity\"\/&gt;\n\n   &lt;\/application&gt;\n&lt;\/manifest&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You\nmust implement onCreate()\nin\nevery activity,\nwhich fires when the system creates your activity. \n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Your implementation should initialise the essential components of your activity: For example, your app should create views and bind data to lists here. Most importantly, this is where you must call <strong>setContentView<code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#setContentView(android.view.View)\" target=\"_blank\" rel=\"noopener\">()<\/a> <\/code><\/strong>to define the layout for the activity&#8217;s user interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Every <code>Activity<\/code> has a life-cycle which is independent of other activities.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img fetchpriority=\"high\" decoding=\"async\" width=\"503\" height=\"614\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/06\/Screenshot-from-2019-06-18-07-09-05.png\" alt=\"\" class=\"wp-image-4889\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/06\/Screenshot-from-2019-06-18-07-09-05.png 503w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/06\/Screenshot-from-2019-06-18-07-09-05-246x300.png 246w\" sizes=\"(max-width: 503px) 100vw, 503px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Methods in activity<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\nONCREATE()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> This method receives the parameter <strong><code>savedInstanceState<\/code><\/strong>, which is a <strong><code><a href=\"https:\/\/developer.android.com\/reference\/android\/os\/Bundle.html\" target=\"_blank\" rel=\"noopener\">Bundle<\/a> <\/code><\/strong>object containing the activity&#8217;s previously saved state. Because if the activity has never existed before, the value of the <strong><code><a href=\"https:\/\/developer.android.com\/reference\/android\/os\/Bundle.html\" target=\"_blank\" rel=\"noopener\">Bundle<\/a> <\/code><\/strong>object is null.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> When <strong><code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onCreate(android.os.Bundle)\" target=\"_blank\" rel=\"noopener\">onCreate()<\/a> <\/code><\/strong>finishes, quickly the next callback is always <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStart()\" target=\"_blank\" rel=\"noopener\"><strong><code>onStart()<\/code><\/strong><\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> ONSTART()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\nThe\n<strong><code><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStart()\" target=\"_blank\" rel=\"noopener\">onStart()<\/a>\n<\/code><\/strong>call\nmakes the activity visible to the user, as the app prepares for the\nactivity to enter the foreground and become interactive.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\nONRESUME()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> This is where therefore user can see the UI clearly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\nONPAUSE()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> From this, either the can goto resume state or navigate to onDestroy() to be destroyed.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"> FRAGMENT <\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>\n\n\tYou\n\tcan think of a fragment as a modular section of an activity, which\n\thas its own lifecycle, receives its own input events, and which you\n\tcan add or remove while the activity is running.\n<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>\n\n\tsort\n\tof like a &#8220;sub activity&#8221; that you can reuse in different\n\tactivities.\n<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"307\" height=\"768\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/06\/Screenshot-from-2019-06-18-07-25-26.png\" alt=\"\" class=\"wp-image-4890\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/06\/Screenshot-from-2019-06-18-07-25-26.png 307w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/06\/Screenshot-from-2019-06-18-07-25-26-120x300.png 120w\" sizes=\"(max-width: 307px) 100vw, 307px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li> To create a fragment, extend the <code><strong><a href=\"https:\/\/developer.android.com\/reference\/androidx\/fragment\/app\/Fragment.html\" target=\"_blank\" rel=\"noopener\">Fragment<\/a> <\/strong><\/code>class, then override key lifecycle methods to insert your app logic, similar to the way you would with an <code><strong><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity.html\" target=\"_blank\" rel=\"noopener\">Activity<\/a> <\/strong><\/code>class. <\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"> FRAGMENT TRANSACTIONS:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"> To move to next fragment, Actually we have to perform few things. For users this is to open new page in the app.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Fundamentally we can use either ACTIVITY or FRAGMENT within a ACTIVITY to display new screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Hence in fragment, we do this<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Fragment newFragment = new ExampleFragment();\nFragmentTransaction transaction = getSupportFragmentManager().beginTransaction();\n\/\/ Replace whatever is in the fragment_container view with this fragment,\n\/\/ and add the transaction to the back stack\ntransaction.replace(R.id.fragment_container, newFragment);\ntransaction.addToBackStack(null);\ntransaction.commit();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you for using pheonixsolutions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you find it useful, share with your friends to keep it alive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fundamental things to know in android activity and fragment Date : 12\/06\/2019 Activity The Activityclass is basically a crucial component [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1021],"tags":[569,577,681],"class_list":["post-4888","post","type-post","status-publish","format-standard","hentry","category-mobile-flutter","tag-android","tag-android-development","tag-fundamental","psol-cat-mobile-flutter"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1gQ","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4888"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4888\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}