From your FirstActivity
call the SecondActivity
using startActivityForResult()
method
(从你的FirstActivity
致电SecondActivity
使用startActivityForResult()
方法)
For example:
(例如:)
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
In your SecondActivity
set the data which you want to return back to FirstActivity
.
(在您的SecondActivity
设置要返回给FirstActivity
。)
If you don't want to return back, don't set any. (如果您不想返回,请不要进行任何设置。)
For example: In SecondActivity
if you want to send back data:
(例如:如果您要发送回数据,请在SecondActivity
:)
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
If you don't want to return data:
(如果您不想返回数据:)
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
Now in your FirstActivity
class write following code for the onActivityResult()
method.
(现在,在FirstActivity
类中,为onActivityResult()
方法编写以下代码。)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…