Moving Between Activities – Intents

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.  But how do we let the system know we want to transition from 1 activity to another?  Enter a new concept: intents.

Sample App Setup

Let’s start off by updating our sampleApp to have two activities.  MainActivity will have a button that says go to Activity 2.  Activity 2 will have a button to go back to MainActivity.  This is what our app now looks like:

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:

Bonus question from the previous blog post:  When I add the second activity, what other file must be updated to let the system know this new activity exists?  The manifest!  I added lines 19-20 to the manifest to ensure the system knows about my new activity:

Add buttons to Layouts

Inside both layout files, I placed one button.  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.  The buttons are colored and text is added. 

Bonus question from the previous blog: Why are the text lines highlighted in yellow, warning you something is wrong?  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 .   

The final, very important thing we did was give each button an ID, so that we can reference them in our code.

Set Logic in Activities

Now that we have the layouts with buttons, we can go into our activities and reference those buttons and add click listeners.  We find the button by calling findViewById().  Then, just set the on click listener:

In this case, we set the button in our main activity to run launchActivity2() when it is clicked.  That function contains the activity transition magic.

On line 21, we can see that we create what is called an intent [1].  Think of an intent as kind of like a plane ticket.  The ticket gives the origin and destination.  So for this intent, we start in the MainActivity (by typing “this”, we are referring to this activity), and we plan to move to the SecondActivity.  Now, myIntent holds our “plane ticket”, but all we are doing at this point is holding the ticket and not actually flying.  To “fly”, or move to the destination, we must call startActivity() and pass in the intent “plane ticket” we created [2].

Now that we can successfully move to the second activity, how do we get back?  We will look at the code for the second activity here:

The setup of the on click listener is identical, but now we are going to run the function goBack() when the button is clicked.  To return to the activity that sent you here, all you have to do is finish the current activity by calling finish() [2].

Sending Data When We Start An Activity

So now you can move to a new activity using an intent and startActivity() .  But what if you want to send data from MainActivity to the second activity?  You can attach that data to your intent:

myIntent.putExtra("amount", 100)

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].  Now, if the new activity wants to access that data, it can use the intent to do so:

getIntent().getIntExtra("amount", 5)

In this example, the key of “amount” and value of 100 was saved in the intent.  The second activity retrieves the intent and calls getIntExtra(“amount”, defaultVal) and the value for the key amount is retrieved (or the default value is returned if no amount exists).  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].

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.  For further reading, here are a few topics worth exploring more:

  • Implicit intents [1]: you can set up intents to move to opening a browser or making a call using phone features outside of your app. 
  • startActivityForResult [2]: Use this when you want the main activity to receive data (results) from the second activity.

And finally, the next logical item to learn will be the topic of my next post – Fragments.

Sources:

[1] “Intent,” Aug 11, 2021 . Accessed on: Oct 13, 2021. [Online]. Available: https://developer.android.com/reference/kotlin/android/content/Intent

[2] “Activity,” Jul 14, 2021 . Accessed on: Oct 13, 2021. [Online]. Available: https://developer.android.com/reference/android/app/Activity

Print Friendly, PDF & Email

Leave a comment

Your email address will not be published.