user6456773 user6456773 - 2 months ago 12
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);
startActivity(i);


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

Answer

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());
startActivity(i);

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

Thanks

Comments