Lessons: 8Length: 44 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Sharing Data Between Activities

If your app has a number of Activities, they are probably related to each other in some way. Most likely, you will want to pass data from one Activity to another. In this lesson, you will learn how to do just that.

Related Links

2.4 Sharing Data Between Activities

In this lesson, we will be learning how to share data between the activities in our Android app. So let us understand what exactly I mean by this. So sharing data. Let's say this is our first activity, and in the text box here, I have a message. This is the feedback message which I want to submit. Let's say I wrote, Happy to be served, and then, on clicking the Submit button, I moved to the next activity. But along with this, I want to take this text of Happy to be served, and show it here in my second activity, which will tell me my submitted feedback. So this way, I want to take my data from the first activity, and on submitting it, I want to show it in my second activity with some modification. So let us move to Android Studio and see how we can do this in our very own demo application. Now let us add the EditText from which the user will enter the data, and then a Button to submit it. In the text tab, I'll change the EditText ID to editText_Feedback. We don't need the inputType PersonName. Instead of text, we will give a hint to the user telling he needs to give the feedback and here we will say button_submit. So this is how our first activity looks. Here, the user will enter the data and then this button will take user to the second activity with the feedback which he has given. So let us implement the function for it. And here, in our MainActivity we have the function ready. Let us write the code, just copy and paste this. And in between this, we need to insert the data which the user has entered. Now let us define the EditText which we have in which the user will enter the feedback. So here we have the EditText initialized. Now we will pass the string in this EditText from our main activity to our second activity. And to do that, i.putExtra, and then the key value pair. The string using which or the key, using which the value will be phased in the second activity. Let's say this is feedback and then the second parameter is the value itself, etfeedback.getString.getText().toString(). So here we have added data to our intent, and then we will just pass this intent to start the second activity. Since this key is a constant for our application, using the best practice we will create a constant class and define it there. So here we have our constant ready, which I have named as KEY_FEEDBACK. And here in MainActivity.java instead of using the hard coded key name I will use the constant which I have created. And here it gives me an error, hitting Alt+Enter the issue is resolved. So we are done with sending the data from the MainActivity. Now what do we do to fetch it in our SecondActivity? For that, first initializing this TextView which we have, which we want to update when the user sends the data. Let us make the variables in the MainActivity as private. Now here, let us fetch the data from the intent. Itent i Alt+Enter, Intent i = getIntent(); and then initialize the Bundle, i.getExtras();. Now, fetch the data from this bundle, User_feedback = b.getString() and the KEY. The KEY which we had was, Constants.KEY_FEEDBACK. Now once we have the user feedback, we will set the text of the TextView to the same. tv.setText("YOUR FEEDBACK:), And then add it. So we are done with fetching the data from the MainActivity and updating our SecondActivity with the data. Now let us run our application and see how well that goes. Now, what feedback should I give? Let's say, Nice Tutorial. On hitting Submit and Yes. We have our data passed from the first activity to the other, so we have successfully learned passing the data from one activity to other. Let's change it to something else. Let's say, Envato is the best, hitting Submit. Here we have the data but yes, there is some issue with the design and the padding that is required in our SecondActivity.xml. Let us provide some padding to our layout. Let's say 20dp. Now thinking of a condition, well let's say this KEY_FEEDBACK is not found or due to some issue, it is not passed from the first activity. So let us give a default value here so that it doesn't give an error. And here we have some warnings, let us deal with it. It says, can be converted to a local variable, Alt+Enter, Convert to Local. Then we have getString may produce NullPointerException in case this bundle is null. So we need to assert that this bundle is not going to be null. So Alt+Enter, and then what it says here is do not concatenate the text displayed with set text. And also it says that string literal inset text cannot be translated. It is giving the warning to use the string localization. I want you to search what string localization is and implement it yourself. I will keep it this way and that was all. In the next lesson, we will see what activity life cycle is and why it is important to deal with it. So stay tuned.

Back to the top