Sending Data Between Screens
The course is part of this learning path
This course explores how to send data from one screen to another in Android applications.
- 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
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 everyone. So, it is now time for this video because we are going to be learning how to, well, you're going to be learning, I'm going to be telling you, suggesting to you, well, in any event, you're going to know by the end of this video how to send data from fragments to an activity. So, the third section of this chapter which is sending data between screens. So, we've done this in the dialog fragment video and if you remember, we created the dialog fragment in the fragment class. We received some data from the user in that fragment.
Later on, we printed the data that we received in the main activity. I'm right but since we are describing data transfer between screens in this section, I did want to mention it here as well because you're ready, you've got the building blocks. Let's go to Android Studio and do some necessary action. So, here you can see I created a new project in Android Studio. And first of all, I just want to show you the finished version. Alright? So, here we have the fragment inside activity and that fragment's name is, well, My Fragment. Inside that fragment, there is a screen like a sign up page, and the user enters a name and an email address.
So, we're going to show these data in the activity after the user clicks the 'SEND DATA' button. So, here I'll write a name and email. So, after I click the 'SEND DATA' button, all this data are sent to the activity. In the Main Activity, these data are received and written on the screen. So, that's what it looks like when you send data from a fragment to an activity. Let's develop that and you'll see what's underneath the hood. I really like that expression because we're not talking about a car. I don't want to look under the hood of a car. Anyway, here we are.
So, let's design the Main Activity and as you can see the main layout is ConstraintLayout. And again, I'm going to change this layout to linear layout from the xml side and I'll write LinearLayout instead of ConstraintLayout. Now, you can also use constraint layout of course, but the design that I showed you earlier is, it's a simple design. You can expand on it any way you want. It will be yours from now on. Anyway, I prefer the Linear Layout. The orientation of the linear layout is going to be vertical and I'll just adjust the gravity property to be center_horizontal. Alright.
So, now we can go to the design area. There is going to be three TextViews, one frame layout in the design area. So, the first text view will be used as a label and that way we'll be able to understand that this design belongs to the activity. So, the other two text views are going to be for printing the name and the email information. So, fragment design will also appear in the frame layout and for that reason, I will now add three TextView components into the design area.
All right. So, I'll select all three TextView components, make the width wrap_content and we'll increase the text size a little bit. And we can also change the text colors. I want the text color to be black and I better leave some space between the TextView components, and so I'll make that margin top value of all three of these TextViews to 15dp. And last thing I'm going to do is increase the size of the first TextView component just a little bit more. So, I'll make that text size 30sp and the text of the first TextView will be Main Activity.
All right. So, the design of the TextView components are now ready. Sure. I've got to add a FrameLayout to the design area. So, we'll change the margin top value of the FrameLayout and it can be 50dp. And let's also change the background of the LinearLayout from the background. Let's have that color. And last up, we should give an id to these components. So, the id of the second TextView will be textViewName. The id of the third TextView will be textViewEmail. And the id of the FrameLayout will be frame.
Now, also in the beginning I really shouldn't write anything in the TextView components here so, I'm just going to be deleting the text of both TextView components. All right. So, that's the design of the main activity. It's now ready. So, now we can go ahead and design the fragment. But first I need to create the fragment. So, I'll select the App folder, and then from the menu I select the File, New, Fragment, Fragment (Blank) and I'll name the fragment MyFragment and click the 'Finish' button to create the fragment.
Okay so, Android Studio now creates a fragment class and a fragment design for us. And here you can see the beautifully created fragment that Android Studio did. So, I'll just customize a little bit, delete the unnecessary sections in the fragment class. So, I'm sure you remember from the previous videos that you have now memorized, fragments have two sections just like an activity: xml and Kotlin class. So, go over to the design of the fragment. First up, I'll delete the TextView. Then in the xml section, I want to change the FrameLayout to Linear. Also, orientation of this LinearLayout, it's going to be vertical. Grab any property of the LinearLayout will be center_horizontal.
So, now we can continue on the design area. So, here I'm going to add a TextView and that's going to show the title under LinearLayout. Then I'll add to text that is, under the Text view to receive the name and the email from the user. First edit text will be the Plain Text. The second edit text will be the email edit text. And I'll need to add a Button to send that data. So, now let's change some properties of these components. First, I'm going to select all of the components and change layout_width property and that will be wrap_content. And put some space between the components, I'm just going to adjust the layout_marginTopvalue to be 20dp. And now, I'm going to just change the text of the TextView component and the text will just be My Fragment. Also, textSize can be 30sp and the textColor let's keep it black. Well, I'm going to keep it black anyway. You can use whatever color you want. I want to select the editText and adjust here with the 300dp.
So, I'm going to delete the text of the editText as well and I want to add a hint to these editText. So, I'll write Enter your name for the first edit text hint. I like that it's called a hint. I'm going to write, Enter your email for the second editText hint. So, now I'm going to change the text of the button and the text will be just Send Data. So here, lastly, I'm going to change the LinearLayout background color. All right. So, that's the really quick down and dirty design of the fragment and it's ready to go. So, we've got to give an id to these components, id of the editText, for name I'll write editTextName; id of the editText, for Email I'll write editTextEmail; id for the button, I'll write buttonSend.
Now, we're ready to write up our code. So, firstly, I'll write the Main Activity codes. I'll just define the components in the main activity lateinit var name : TextView. late var email : TextView. Then, you got to go inside onCreate method name = findViewByid(R.id.textViewName) email = findViewByid(R.id.textViewEmail). All right, now I'm going to create an object from the fragment manager class, and I'll write val fragmentManager : FragmentManager = supportFragmentManager. Then, I'll create an object from the fragment transaction class. I'll write val fragmentTransaction : FragmentTransaction = fragmentManager.begintransaction(). Then, I'll create an object from the my fragment class, and I'll write val myFragment = MyFragment(). Okay so, now I can write fragmentTransaction.add So, the add method will take two parameters. First parameter is, well,
I define the component which I want to show the fragment. So, in that particular example, I want to show the fragment inside of the frame layout. That means I'm going to write R.id.frame and for the second parameter I will write the object's name which I created from the class of MyFragment.
All right. So, after that I'll write fragmentTransaction.commit(). And that's the way that we add the fragment to the Main Activity. Wow, that was quite the ride. So, if you want to, we can take a short break but in any event, we're going to continue on in the next video. All right, I'll see you then.
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.