Trong hoạt động thứ hai của bạn, bạn có thể lấy dữ liệu từ hoạt động đầu tiên với phương pháp getIntent()
và sau đó getStringExtra()
, getIntExtra()
...
Sau đó, quay trở lại hoạt động đầu tiên của bạn, bạn phải sử dụng phương thức setResult()
với dữ liệu ý định để trả về dưới dạng tham số.
Để nhận dữ liệu trả về từ hoạt động thứ hai của bạn trong hoạt động đầu tiên, chỉ cần ghi đè phương thức onActivityResult()
và sử dụng ý định lấy dữ liệu.
Hoạt động đầu tiên:
//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");
}
Hoạt động thứ hai:
//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
Đừng quên để bắt đầu hoạt động thứ hai của bạn với phương pháp startActivityForResult()
.
bạn có thể chọn tùy chọn hoặc đặt phương thức bổ sung được đưa ra dưới đây. – itsrajesh4uguys
sử dụng startActivityForResult để gọi một hoạt động khác và nhận kết quả trên onActivityResult –