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

Sending Data from Fragment to Activity - Part 2

Start course
1h 27m

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.


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


Alright. Hello friends. So, previous video, where we left off? We created the fragment. We completed the application design. We also wrote the necessary codes to add the fragment to the activity. So, in this video, we're going to write the necessary codes in the fragment class. So, drivers start your engines. Oops, there's that under the hood analogy again. Anyway, I am going to open 'Myfragment.kt'. So, first I wanted to find the components that I added to the fragment. So, I'll write lateinit var name : EditText. For the second EditText, lateinit var email : EditText. And I wanted to find the buttons, so I'll write lateinit var send : Button. 

Okay, so now we can continue inside the onCreateView function. So, what we need to do in here is create an object from the view class, inflate the fragment design to this object. So for that, I'll write val view : View = and I'll cut the line of code after the return keyword, paste it to the right of the = sign. And of course, we should return the view object. Now, we can use the find view by id method using the view object. So, name = view.findViewById(R.id.editTextName), email = view.findViewById(R.id.editTextEmail), send = view.findViewById(R.id.buttonSend). All right, what comes next? Adding a ClickListener to the button because you know, you know, you know the process of sending data should be done inside the ClickListener of the button.

Okay, so I need to get the data that the user enters and add all that to the string container. So, I'm going to write val userName = name.text.toString(), and I'll get the email like that as well. So, val userEmail = email.text.toString(). All right, so now we can send the received data to the main activity. So, the basis of the process of sending data from fragment to activity is create a method within the activity to which you will send the data and access this method and send the relevant parameters to the activity. Now I know we haven't created a method in the MainActivity yet.

So let's, first create a method in MainActivity. Alright, so this is where we're going to get this data. So, I want to just move on over to the MainActivity again. Now, outside the onCreate function, I'm going to write a new function. So, what do I write? funtakeData(). So, also this function is going to take two parameters, because we're going to be able to send the data from the fragment to activity by using these parameters. The first parameter has got to be (userName:String). Second parameter that's going to be the userEmail : String.

All right, so now I can call this function from inside the fragment. All right, in order to send data, we must access this takeData function, all right. We've already created it in the MainActivity. So, now we will create an object from the MainActivity. But since we're going to create this object in the fragment class, we're also going to use the activity function from the fragment activity. You understand? So, let's just write the necessary code without any further explanation, because, you know what, here I am creating an object from the MainActivity class, val mainActivity : MainActivity = you see where we're going to access the function in the class where we define the fragment, I write, activity just to the right of the = sign. Notice that this is a function of the fragment activity. If we want to access the activity in which we define the fragment from within the fragment, this is the way you do it.

But wait, what's this? Android Studio throws a warning. Don't you know I'm doing a video here. I'm just kidding, because all you have to do is click the 'red lamp', and select 'Cast expression 'activity' to the 'MainActivity'', that is the option for the cast process. So, here Android Studio adds the MainActivity after the activity using the as keyword. So, it specifies that this activity, this one right here is the MainActivity  that is inside our project packet. Alright, so I'm going to send this data  using the MainActivity object which I just created. So, after writing mainActivity.takeData(), I'm going to write arguments userName, userEmail. So, you can see here, right? That sending data from fragment to activity is not the same as sending data from activity to fragment.

Let's just think about that for a moment. See, obviously if it was the same, there wouldn't actually even be a need for me to be sitting here doing this video for you. You just got to cement that in your head, and we're done. But additionally, we could write the two-line code that we wrote here in a single-line as follows: (activity as MainActivity) you follow? Okay, so outside the parentheses I'll write .takeData(userName, userEmail). Not cool, because with the activity we use here, we are actually pointing to the activity where we define the fragment, but since activity here is an element of fragment activity, we'll also have to cast using this as keyword if you want to send data from fragment to activity. So, you can use two different methods just like you saw us using here. The option is yours. So, that completes the process in the fragment section. But you might have noticed, we didn't fill that methods inside yet. So, we got to do that in the MainActivity, all right. So, I want to go back to the MainActivity, and in the takeData function, I will write name.text = userName, and in the next line I'll write email.text = userEmail. Alright, alright, so everything's ready.

Now, we can run the application. So, it opens and I just want to fill in the name and email sections. So, after clicking the 'SEND DATA' button, data gets sent to activity. And it's written on the TextViews. Alright, my friends, that's how you send data from fragment to activity. That's pretty cool, I think. I hope you have fun with it. In the next video, we're going to send data from fragment to fragment, and you're thinking, I already know. Let me do it. All right, go ahead. I'm going to take a break. See you in the next video.

About the Author
Learning Paths

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