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

Sending Data Between Fragments

Start course
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

And we're back. Hello everyone. In this video we are going to send data from fragment to fragment which is the fourth chapter of sending data between screens. So, we're going to move on over to Android Studio and continue. Yes. So, I've created yet another new project in Android Studio. Here, there's going to be two fragments but first I want to show you the finished version so, you see where we're going.

So, look inside the main activity there's a fragment. Fragment's name is First Fragment. There's an edit text to enter the name inside that first fragment. So, I enter the name, click the 'Send Data' button. Second fragment opens and the name can be seen inside.   Now, the second fragment's name is Second Fragment. So, after I click on the 'Send Data' button, the second fragment opens and the data is written, just the way I wrote it in. Okay so, that's what the example looks like. Let's do it up. So, first in Android Studio, I want to create the first fragment. Now, you know how to create a fragment. Did you already do it? Or I can just quickly go over it with you.

So, first fragment for the name click the 'Finish' button and that way Android Studio creates the first fragment. Delete the unnecessary and. Now, create the second fragment. So, Second Fragment as the name and click the 'Finish' button. So, that creates the second fragment. So, now let's design the first fragment. So, here I just want to delete the frame layout and write constraint layout. Maybe I threw you for a loop there. Typically, we will use linear layout but I want to use  the constraint layout in this video just because.  So, if I adjust a layout height and layout  width properties of the text view, I can also write the first fragment of the text for the text view. Now, we can understand that this is indeed the first fragment. And I'll do the regular stuff like changing the text size of the text view to be 30sp. Also, the text color can be black. Okay so, now I add a button and one edit text on the design area here and the edit text will be plain text.

So, I'll add a button then I'll locate these three components on the constraint layout. I will select all of these components and right click and select 'Center', 'Horizontally'. Also, I'll need to determine the vertical constraints. So, the top constraint value of the text  view  could be 30. The top constraint value of the edit text can be 50 and the top constraint value of the button can be 30dp. I'm going to need to delete the text of the edit text and I'm going to add a hint. The hint will be 'Enter your name'. So, the width of the edit text, I'm going to make that 300dp. So, I'm going to change the text of the button and the text can be 'Send Data'. So, I want to give an id to these components. I will write editTextName for the id of the editor. I will write buttonSend for the id of the button. Let's have a look at the second fragment design. Now in this fragment there's already a text view so, I just need to adjust the height and the width of the text view to be wrap_content and I'm going to change the text of the text view. That will be second fragment.

So, also I want to increase the text size and the text size could be 30sp and also text color should be black in my estimation. So, now we can change the frame layout. So, I want to go into the xml section, delete the FrameLayout and write ConstraintLayout. Now, I can continue with the design section. I want to add one more text for you to make it right the data that we receive.  So, I'll increase the font size a little bit. I'm going to make it 24sp and of course the color of the text view is going to be black. Also, since the main layout is the constraint layout, I want to determine the constraint values of these components. So, I just want to select both text views right click and select 'Center', 'Horizontally', snap to it. So, now I need to determine the vertical constraints. Top constraint value of the first text view could be 30. Top constraint value of the second text view can be 20. Okay, so now I'm going to give an id to the second text view. The id of this component can be TextViewName and then I want to change the background color of the constraint layout so that you can see the difference of the fragment. That would be way more noticeable. So, we finished the design of both fragments.

So, now I want to design the main activity. So, I want to go and open the activity_main.xml file. Now there will only be one frame layout in the main activities design area. And we're going to display the fragments in this frame layout. So, for this I need to add a FrameLayout to the design area. So, you know that the FrameLayout is going to take up the entire screen pretty much so, I'm going to make the width and the height of the FrameLayout match_parent. And I will also determine the constraint values of the FrameLayout so, I will pin it to the top, to the right, to the bottom, and the left. Okay so, I got to give an  id to this FrameLayout and I'll write frame for its id. Okay, so that's the design of the application. Now, we can go ahead and write some code. First fragment should appear to the user when the application is first launched. So, I will add the firstFragment to MainActivity. To do that I'll open up the MainActivity.kt file and here I'll create an object from the fragmentManager class. val fragmentManager : FragmentManager = supportFragmentManager. Got to be used to that by now. And then I can create an object from the fragmentTransaction class.

So, I will write  val fragmentTransaction : FragmentTransaction = fragmentManager.beginTransaction(). So, now I can create an object from the first fragment  class and I will write val firstFragment = FirstFragment() and I will continue by writing fragmentTransaction.add(). So, this add method will take two parameters. First one is the component's id which we're going to want to show inside the fragment. So, I will show inside the frame layout, so I will write (R.id.frame, firstFragment). So, I'll finish this process with the fragmentTransaction.commit(). So, that way when we open up the application, the firstFragment will run inside the MainActivity. So, we got to give some love to that firstFragment. The design is done but now we've got to write its code. So, I first wanted to define the components. Inside the onCreate view function val view : View =, cut this code and paste it here and I will return view object.

Now, it's time to define the edit text. So, I'll write val name : EditText = view.findViewById(R.id.editTextName) and I'll define this send data by val send : Button = view.findViewById(R.id.buttonSend). What to do next? That's right add a ClickListener to the button. So, I'll take this data inside the ClickListener which the user enters in the user name section and send it to the second fragment. So, we'll get the data and then assign it to a string. And for that I will write val userName = name.text.toString() Here I'm going to use the bundle class for the sending process. First, I'll create an object from the bundle class. I will write val bundle = Bundle() Then I will send the data using the bundle method.

So, I'm going to write bundle.putString() and the putString method gets two parameters: write in keyword and data to send. And I'll write username for the keyword, and data to be sent is userName. All right, very good. So, now it's time to create an object, the secondFragment. So, I'll write val secondFragment = SecondFragment(). Then with a bundle again, I'll specify that data will be sent to the secondFragment. So, I'm going to write secondFragment.arguments =  after the = sign I'll write the bundle object.

Now, by the way, while data is being sent, the second fragment's got to be open. So, we've got to make that process here too. So, the opening of the second fragment will be the same as how we run the first fragment actually in the main activity. So, I just want to write it in here, val fragmentManager : FragmentManager = now we can't directly use supportFragmentManager here. So, for this we've got to first use the requireActivity() from fragment activity. You see, now we can use supportFragmentManager, and I'll write .supportFragmentManager. Then I can write val fragmentTransaction : FragmentTransaction = fragmentManager.beginTransaction(). And that means we must run this second fragment.

I'll write fragmentTransaction.replace() for the method. All right, this time we'll use the replace() method instead of remember the add() method, because there's already a first fragment in the frame layout. So, if we use the add() method, well, the second fragment is just going to be overwritten by the first fragment. And what does that do? Well, that'll just result in complex image. But with replacement, the second fragment will be displayed in the frame layout instead of the first fragment. I will show the second fragment again once we're inside the frame layout at the main activity. So, up in here I will write (R.id.frame,) and that way the first fragment will close, second fragment will open, and that way I get to run two fragments in the main activity.

So, the second parameter is this secondFragment object. And then finally I'm going to write fragmentTransaction.commit() All right, so that sends the data to the second fragment and the second fragment opens. So, now we must get that sent data from the second fragment. So, we'll move on to the second fragment and continue. So, I want to create an object from the view class, val view : View = and I'll cut this code, paste it here, So, now we can define the TextView using the view object. val name : TextView = view.findViewById(R.id.textViewName) All right, then I'll get that data that's sent with the bundle method. And here I will write arguments.getString, keyword is username, and lastly .toString().

Also, I'll need to show the data on the TextView. So, for this at the very beginning of the code I want to write the name.text = and for safe I will put ? after the argument. All right, so with that the process is completed. Let's have a look when we run the application. What's going to happen? All right, so, application opens, first fragment's running. Okay, so I'm going to enter a name, and click the 'SEND DATA' button. All right, so when I click on the 'SEND DATA' button, the second fragment also opens and the name is up here, perfect. All right, so that's sending data from one fragment to another fragment. There's more to do. So, I'll see you in the next video.

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