How to transfer data from one activity to another in android

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

// I assume Page.class is your second activity
Intent intent = new Intent(this, Page.class); 
intent.putExtra("arg", getText()); // getText() SHOULD NOT be static!!!
startActivity(intent);

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

String passedArg = getIntent().getExtras().getString("arg");
enteredValue.setText(passedArg);

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

public static final String ARG_FROM_MAIN = "arg";

Leave a Comment