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

Sending Data from Activity to Fragment - 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, my friends. So, in our previous video, sure we made the necessary design and we've gotten the data from the user, we sent it to the fragment class. So, that means in this lesson, we're going to take all that data that was sent from the fragment class, calculate the BMI value, and print it to the TextView component. So, let's get to it. We've defined the idea of the TextView which we'll use on the fragments. So, we'll define that TextView in the fragment class. And I'll write lateinit var result: TextView. So, I'll continue in the onCreateView method. Now, do you remember in order to use the findViewById method here, we had to access the fragment design first?

So for this, we'll just need to create an object from the View class and inflate the fragment designed to this object. And that's why I'm writing here, val view: View = Now, here I'll just cut the line of code after the return keyword and paste it to the right of the equal sign. And, we should return the view object. So, now we can use the findViewById method using this view object. So, let's match the TextView with its Id. I'll write in: result = view.findViewById(R.id.textViewResult). Now, we can get the data that we sent from the activity. And for this, we'll use the arguments keyword. So, I'll write in arguments.getInt(). Now, if I was supposed to get string data, I would have used the method of getString(). So, we should also write the keyword that we used while sending the data in parenthesis.

So first, let's take the weight value that came from the activity. So for this, I'll write weight as the keyword, and of course, it's going to be better if we pass it to a variable. So, I'll write, val weight at the beginning of the code. Now, of course, you notice the warning here, because the data may be null. So, that's why I'm just adding a ?. Alright, so now, the weight entered by the user in the edit text is taken in the fragment class and I've got to change it to an integer. So, I will add the toInt() function at the end of the code. Of course, you may notice there is a warning here. Here, we need to prevent my app from crashing in the case of returning null, a double ! here. So, with the same process, we can get the user's height. So, val height = arguments?.getInt(height)!! And I've got to change it to an integer, so, I will add the toInt() function at the end of the code.

All right, so we got the data. Let's calculate BMI with the data that we got. There is a formula to calculate BMI, by the way. So, the formula is (user's weight * 10000) / (height  * height.) So, we'll see a fractional number as a result of that calculation. And, we must match that value with a double container. So, I write val bmi: Double = at the beginning of the code. And also, I've got to write the .toDouble() function at the end of the code. So now, by using the sent data, we've calculated the BMI. So, we've got to write this result now to the TextView. So, I'll write result.text after the equal sign, I'll write Your BMI is $bmi. So, in that way the result has been written. Now, we can run the application and see what it looks like. Okay, so the application opens, I'll enter in a weight value and then some height. Now, after clicking the 'Calculate' button that calculated the results and wrote it out, I'm feeling a little better about myself. Anyway, that's how to send data from activity to fragment. We're going to put it more into practice in the next video. I'll see you there.

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