In your second activity, you can get the data from the first activity with the method getIntent()
and then getStringExtra()
, getIntExtra()
...
Then to return to your first activity you have to use the setResult()
method with the intent data to return back as parameter.
To get the returning data from your second activity in your first activity, just override the onActivityResult()
method and use the intent to get the data.
First Activity:
//In the method that is called when click on "update"
Intent intent = ... //Create the intent to go in the second activity
intent.putExtra("oldValue", "valueYouWantToChange");
startActivityForResult(intent, someIntValue); //I always put 0 for someIntValue
//In your class
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Retrieve data in the intent
String editTextValue = intent.getStringExtra("valueId");
}
Second Activity:
//When activity is created
String value = intent.getStringExtra("oldValue");
//Then change the editText value
//After clicking on "save"
Intent intent = new Intent();
intent.putExtra("valueId", value); //value should be your string from the edittext
setResult(somePositiveInt, intent); //The data you want to send back
finish(); //That's when you onActivityResult() in the first activity will be called
Don't forget to start your second activity with the startActivityForResult()
method.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…