简体   繁体   中英

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!

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

You have to attach a message to the value you're trying to pass: i.putExtra("STRING_I_NEED", strName); So you want to put inputuserName.getText()).toString() where strName is.

In Next activity: Bundle extras = getIntent().getExtras(); newString= extras.getString("STRING_I_NEED");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM