{"id":24,"date":"2021-10-13T22:56:57","date_gmt":"2021-10-13T22:56:57","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/workla\/?p=24"},"modified":"2021-10-13T23:36:11","modified_gmt":"2021-10-13T23:36:11","slug":"moving-between-activities-intents","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/workla\/2021\/10\/13\/moving-between-activities-intents\/","title":{"rendered":"Moving Between Activities &#8211; Intents"},"content":{"rendered":"\n<p>In my last blog post, we learned about the basic file types, and that when beginning with Android app development, if we want two different screens, we are going to make two different activities with their corresponding layout files.\u00a0 But how do we let the system know we want to transition from 1 activity to another?\u00a0 Enter a new concept: <strong>intents<\/strong>.<\/p>\n\n\n\n<div style=\"height:48px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Sample App Setup<\/strong><\/h2>\n\n\n\n<div style=\"height:43px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Let\u2019s start off by updating our sampleApp to have two activities.&nbsp; MainActivity will have a button that says go to Activity 2.&nbsp; Activity 2 will have a button to go back to MainActivity.&nbsp; This is what our app now looks like:<\/p>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"577\" height=\"961\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/appWithButtons-1.gif\" alt=\"\" class=\"wp-image-46\" \/><\/figure>\n\n\n\n<div style=\"height:45px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>So in order to modify our first app with one activity to create this app with two activities, I added a second activity file and a second layout file:<\/p>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"443\" height=\"1024\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/new-activities-in-folders-443x1024.jpg\" alt=\"\" class=\"wp-image-26\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/new-activities-in-folders-443x1024.jpg 443w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/new-activities-in-folders-130x300.jpg 130w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/new-activities-in-folders.jpg 505w\" sizes=\"auto, (max-width: 443px) 100vw, 443px\" \/><\/figure>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Bonus question from the previous blog post:\u00a0 When I add the second activity, what other file must be updated to let the system know this new activity exists?\u00a0 <strong>The manifest!\u00a0<\/strong> I added lines 19-20 to the manifest to ensure the system knows about my new activity:<\/p>\n\n\n\n<div style=\"height:43px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"996\" height=\"707\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/manifest-1.jpg\" alt=\"\" class=\"wp-image-27\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/manifest-1.jpg 996w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/manifest-1-300x213.jpg 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/manifest-1-768x545.jpg 768w\" sizes=\"auto, (max-width: 996px) 100vw, 996px\" \/><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Add buttons to Layouts<\/h2>\n\n\n\n<div style=\"height:39px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Inside both layout files, I placed one button.\u00a0 For the sake of brevity, I am not going explain constraints for now, but just know that the constraints in this code snippet place the buttons in the middle of the screen.\u00a0 The buttons are colored and text is added.\u00a0<\/p>\n\n\n\n<div style=\"height:33px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"791\" height=\"696\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/activity-main-xml.jpg\" alt=\"\" class=\"wp-image-28\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/activity-main-xml.jpg 791w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/activity-main-xml-300x264.jpg 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/activity-main-xml-768x676.jpg 768w\" sizes=\"auto, (max-width: 791px) 100vw, 791px\" \/><\/figure>\n\n\n\n<div style=\"height:33px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"733\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/second-activity-xml.jpg\" alt=\"\" class=\"wp-image-29\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/second-activity-xml.jpg 816w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/second-activity-xml-300x269.jpg 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/second-activity-xml-768x690.jpg 768w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/figure>\n\n\n\n<div style=\"height:41px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Bonus question from the previous blog: Why are the text lines highlighted in yellow, warning you something is wrong?\u00a0 The answer: Remember that string should not be typed directly in the layout or activity files. Rather, you should place these strings in the strings.xml file in res\/values and then reference the string here with @string\/whateverINamedMyStringResource . \u00a0\u00a0<\/p>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The final, very important thing we did was give each button an ID, so that we can reference them in our code.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Set Logic in Activities<\/h2>\n\n\n\n<div style=\"height:39px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Now that we have the layouts with buttons, we can go into our activities and reference those buttons and add click listeners.\u00a0 We find the button by calling findViewById().\u00a0 Then, just set the on click listener:<\/p>\n\n\n\n<div style=\"height:31px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1032\" height=\"765\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/mainactivity-1.jpg\" alt=\"\" class=\"wp-image-30\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/mainactivity-1.jpg 1032w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/mainactivity-1-300x222.jpg 300w\" sizes=\"auto, (max-width: 1032px) 100vw, 1032px\" \/><\/figure>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In this case, we set the button in our main activity to run launchActivity2() when it is clicked.&nbsp; That function contains the activity transition magic.<\/p>\n\n\n\n<div style=\"height:33px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>On line 21, we can see that we create what is called an intent [1].\u00a0 Think of an intent as kind of like a plane ticket.\u00a0 The ticket gives the origin and destination.\u00a0 So for this intent, we start in the MainActivity (by typing \u201cthis\u201d, we are referring to this activity), and we plan to move to the SecondActivity.\u00a0 Now, myIntent holds our \u201cplane ticket\u201d, but all we are doing at this point is holding the ticket and not actually flying.\u00a0 To \u201cfly\u201d, or move to the destination, we must call startActivity() and pass in the intent \u201cplane ticket\u201d we created [2].<\/p>\n\n\n\n<div style=\"height:36px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Now that we can successfully move to the second activity, how do we get back?&nbsp; We will look at the code for the second activity here:<\/p>\n\n\n\n<div style=\"height:36px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"922\" height=\"660\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/activity-2.jpg\" alt=\"\" class=\"wp-image-38\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/activity-2.jpg 922w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/activity-2-300x215.jpg 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/4737\/files\/2021\/10\/activity-2-768x550.jpg 768w\" sizes=\"auto, (max-width: 922px) 100vw, 922px\" \/><\/figure>\n\n\n\n<div style=\"height:37px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The setup of the on click listener is identical, but now we are going to run the function goBack() when the button is clicked.\u00a0 To return to the activity that sent you here, all you have to do is finish the current activity by calling finish() [2].<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Sending Data When We Start An Activity<\/h2>\n\n\n\n<div style=\"height:36px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>So now you can move to a new activity using an intent and startActivity() .&nbsp; But what if you want to send data from MainActivity to the second activity?&nbsp; You can attach that data to your intent:<\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>myIntent.putExtra(\"amount\", 100)<\/code><\/pre>\n\n\n\n<div style=\"height:45px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>By adding a key value pair using putExtra(), you are attaching that info to the intent that will be passed to the new activity [1].&nbsp; Now, if the new activity wants to access that data, it can use the intent to do so:<\/p>\n\n\n\n<div style=\"height:41px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>getIntent().getIntExtra(\"amount\", 5)<\/code><\/pre>\n\n\n\n<div style=\"height:41px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In this example, the key of \u201camount\u201d and value of 100 was saved in the intent.\u00a0 The second activity retrieves the intent and calls getIntExtra(\u201camount\u201d, defaultVal) and the value for the key amount is retrieved (or the default value is returned if no amount exists).\u00a0 If instead, MainActivity wanted to send a key value pair whose value was a string or a float etc, the only change would be that within the second activity, the call for getIntExtra() should change to getStringExtra(), getFloatExtra, etc [1].<\/p>\n\n\n\n<div style=\"height:31px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This blog shows the simplest way to move back and forth between activities and to send data from the first activity to the second, but there are more advanced options available beyond the scope of this introductory post.&nbsp; For further reading, here are a few topics worth exploring more:<\/p>\n\n\n\n<div style=\"height:38px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Implicit intents [1]<\/strong>: you can set up intents to move to opening a browser or making a call using phone features outside of your app.\u00a0<\/li><li><strong>startActivityForResult [2]<\/strong>: Use this when you want the main activity to receive data (results) from the second activity.<\/li><\/ul>\n\n\n\n<div style=\"height:43px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>And finally, the next logical item to learn will be the topic of my next post \u2013 Fragments.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Sources:<\/p>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>[1] \u201cIntent,\u201d Aug 11, 2021 . Accessed on: Oct 13, 2021. [Online]. Available: <a href=\"https:\/\/developer.android.com\/reference\/kotlin\/android\/content\/Intent\">https:\/\/developer.android.com\/reference\/kotlin\/android\/content\/Intent<\/a><\/p>\n\n\n\n<div style=\"height:28px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>[2] \u201cActivity,\u201d Jul 14, 2021 . Accessed on: Oct 13, 2021. [Online]. Available: https:\/\/developer.android.com\/reference\/android\/app\/Activity<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last blog post, we learned about the basic file types, and that when beginning with Android app development, if we want two different screens, we are going to make two different activities with their corresponding layout files.\u00a0 But how do we let the system know we want to transition from 1 activity to&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.oregonstate.edu\/workla\/2021\/10\/13\/moving-between-activities-intents\/\">Continue reading <span class=\"screen-reader-text\">Moving Between Activities &#8211; Intents<\/span><\/a><\/p>\n","protected":false},"author":11613,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/users\/11613"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":10,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":52,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/posts\/24\/revisions\/52"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/workla\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}