1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Sending Data Between Screens in Android App Development

Sending Data from Activity to Activity - Part 2

Sending Data from Activity to Activity - Part 2
Overview
Difficulty
Intermediate
Duration
1h 27m
Students
5
Description

This course explores how to send data from one screen to another in Android applications.

Learning Objectives

  • Learn how to send data from one activity to another activity
  • Learn how to send data from activity to fragment
  • Learn how to send data from fragment to activity
  • Learn how to send data from one fragment to another fragment

Intended Audience

This course is intended for anyone who wants to learn how to start building their own apps on Android.

Prerequisites

To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.

Transcript

Hello everyone. Didn't see you sitting there. Okay so, in the previous video we designed the main activity, right? We wrote the necessary code for sending data. So, that means in this video we're going to create the second activity. We'll get the data into the second activity and then print it to the text views. So, what are you doing sitting there still? Let's get to Android Studio. So, first of all, let's create the second activity. So, for that I just select the 'app' folder, then 'File' 'New', 'activity', 'Empty activity'. Name the activity and all. I started intent with the Name second activity, remember? So, I will use the same name, obviously. And I'm going to click 'Finish.' So, as you can see after creating the second activity, the warning disappears. Now let's make the design of the second activity. So, first I just want to change the 'ConstraintLayout' to 'LinearLayout', and I'll adjust the value, the orientation to be 'vertical'. All right, so now I can add three text views to linear layout.

So, I'll choose all text views and adjust their width to be wrap content. Also, text color can be black of these components. Text size can be twenty 20sp. All right. Now, just choosing the 'LinearLayout', adjust the value of the 'gravity' to be 'center'. All right. Why don't we give some space between the text views? layout_marginTop value for the second and the third text, you can be 30dp. And then I got to give an I.D to these texts views to wrap this up. So, I want to write 'textViewName' and that will be for the first text view. Second, I will write 'textViewEmail'. And for the third one, I will write 'textViewPhone'. All right. So, that's done. Now, let's define these components in Kotlin. So, I'll write lateinit var_tvName : TextView, lateinit var_tvEmail : TextView, lateinit var_tvPhone : TextView So, then in the on create method, I'll match them with their ID's tvName = findViewById(R.id.textViewName) tvEmail = findViewById(R.id.textViewName) tvPhone = findViewById(R.id.textViewPhone).

All right. So, now we've got to get the information which we sent before. So, we're going to do the process of getting the data with the intent method. All right. So, I'll get the name. First, so I will write val name = intent.getStringExtra. So, here to be able to get string data we use the getStringExtra method. Follow? So, inside of this method I'll just write the keyword. So, for name, I will write 'name' because that's what I identify the keyword to be. That's why I like kiss. You know, KISS? Keep It Simple Stupid. Keep it simple stupid. That way, you're consistent. Anyway, for name, I identify the keyword to be name. Remember, if the keywords don't match, they are not going to get the data.

Now for the email I will write val email = intent.getStringExtra Inside the parenthesis, I will write, email is a keyword. And last up, get the phone number. Now if you remember, we send the phone number as along. So that means after writing val phone, I will write intent.getLongExtra. Now to be able to get a long value, we use the get long extra method, right? Now the get long extra method, of course takes two parameters. The first one is the keyword, so I will just write phone. The seond one is the default value. The default values, of course, the value to be assigned if no information is received or an error occurs. Thank you very much. Anyway, here you can write anything you want, literally, because it's what you're going to see when an error occurs. Typically, I just write zero as the default value.

All right. So, just like that will get the data. Now let's write these data to the text views. So, I will write tvName.text =, and here I'm going to add the 'Hello' word before the name just like you saw earlier. So, all I do is after writing hello inside double quotes, I'll use the dollar sign. After the dollar sign, I write name. Right? So, in that way the word Hello will be constant. And near it there will be the name that was, well, entered by the user. All right. So, by using the dollar sign, we'll define the variables inside the double quotes. All right. Now, we've got to print other data to the text view. So, tvEmail.text = "". And inside the double quotes, on this write

your email address is $email. And last up, tv.Phone.text = " " and inside the double quotes, I'll write your phone number is $phone. All right. So, the application is ready. Let's run it and see. Application opens. So, I will enter name and an email address and a phone number. So, what happens after clicking the sign up button? Data gets sent to the second activity. In the second activity, it's all written in the text views. So, just like that, right? Anyway, we've learned how to send data from one activity to another activity. Of course, you don't have to show the user every piece of information that you get or send. The important thing is learning how to send data. So, in the next video, we're going to learn how to send data to fragments where we will take them from the activity. All right, so I want to see in the next lesson.

 

About the Author
Students
86
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics