How to transfer data from one activity to another in android

You need to change

 static TextView textView;
 textView = (TextView) findViewById(R.id.editText1);

to

 EditText ed1; 
 ed1 = (EditText) findViewById(R.id.editText1);

Coz you have

<EditText
android:id="@+id/editText1" // it is edittext not textview

Then

public void transferIT(View view){
String value = ed1.getText().toString()
Intent intent = new Intent(this, Page.class);
intent.putExtra("key",value);
startActivity(intent);
}

Then in onCreate of second activity

String value = getIntent().getExtras().getString("key");

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";

You send the data in the intent when you call the second activity. This is pretty fundamental stuff. I suggest you read up on Intents and Parcelable concepts in Android and Serialization in Java that are all related to your question.

Tags:

Java

Android