I got a situation like this:
Activity A --> B --> C --> D, when D finished, I have to refresh ActivityA to display what I input in ActivityB , ActivityC and ActivityD.Here is my code:
ActivityA
@OnClick(R.id.btn_one)
public void toActivityB(){
Intent intent = new Intent();
intent.setClass(this, ActivityB.class);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK){
String b = data.getStringExtra("b");
String c = data.getStringExtra("c");
String d = data.getStringExtra("d");
tvAll.setText(etOne.getText().toString() + "
" + b + "
" + c + "
" + d);
}
}
ActivityB
@OnClick(R.id.btn_two)
public void toActivityC(){
Intent intent = new Intent();
intent.setClass(this, ActivityC.class);
startActivityForResult(intent, 2);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == RESULT_OK){
data.putExtra("b", etTwo.getText().toString());
setResult(RESULT_OK, data);
finish();
}
}
ActivityC code just like ActivityB, the last ActivityD as follow:
@OnClick(R.id.btn_four)
public void returnToActivityA(){
Intent intent = new Intent();
intent.putExtra("d", etFour.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
In this way, I can get the input value in ActivityB,ActivityC,ActivityD.but I have to override the method onActivityResult
at every Activity and handle the data one by one.Is there any another easy way for me to handle this situation?Any good suggestion I'll be appreciate.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…