Skip to content
Advertisement

How to transfer data from one activity to another in android

It might sound simple but I can’t make it working. I have two activities. Which first one is a form, and second one is showing data from a JSON file based on values entered in the first activity.

So I am trying to make a simple version of it. I have a EditText, and a button, so when they press the button, whatever was in the EditText will be in the TextView of the next activity.

Here is my code so far: Main Activity

JavaScript

Main XML

JavaScript

Second Activity

JavaScript

Second XML

JavaScript

In this way I made a method for the data of the EditText and in the other activity I call it to get access to it. Currently when I press the button it stops working. So what is your solution for having the data of the form in other activity ?

Advertisement

Answer

In the first activity you should put the extra argument to intent like this:

JavaScript

Then in the second activity, you retrieve the argument like this:

JavaScript

It’s also good to store the arg String in MainActivity as constant and always refer to it in other places.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement