user6456773 user6456773 - 1 year ago 45
Android Question

How to take the EditText's value from one activity in another?

There are two activities. I want to take the entered in the EditText field dat from the first screen into the second one. I used this code in the first activity:

Intent i = new Intent(this.getApplicationContext(),MainActivity.class);
i.putExtra((inputuserName.getText()).toString(), true);

How can access the value in the second one? Thanks!

Answer Source

hi why you trying to send boolean type true or false ? You need to send only String value. See,

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
i.putExtra("STRING_I_NEED", inputuserName.getText()).toString());

Then, to retrieve the value try something like:

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");