You need to start the second activity with startActivityForResult
:
In onActivityResult
, you should also check the resultCode
is RESULT_OK
, rather than the requestCode
.
質問
I have an activity that Call another activity for filling an address and second activity should send back the address for the first activity and show it in a textview in first activity
i used these codes but i dont know why it s not working
first activity:
Intent in = new Intent(getApplicationContext(),ShippingActivity.class);
startActivity(in);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_OK){
tvDeliverTo.setText(data.getStringExtra("DeliveryAdressKEY"));
}
}
and second activity
Intent in = new Intent();
in.putExtra("DeliveryAdressKEY", tvAdress.getText().toString());
setResult(RESULT_OK, in);
finish();
thanks in advance
解決
You need to start the second activity with startActivityForResult
:
In onActivityResult
, you should also check the resultCode
is RESULT_OK
, rather than the requestCode
.
他のヒント
I believe you should be using startActivityForResult() instead of startActivity().
first activity
Intent in = new Intent(getApplicationContext(),ShippingActivity.class);
startActivityForResult(in, 0);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode ==1){
tvDeliverTo.setText(data.getStringExtra("DeliveryAdressKEY"));
}
}
}
and second activity
Intent in = new Intent();
in.putExtra("DeliveryAdressKEY", tvAdress.getText().toString());
setResult(1, in);
finish();
and now is working