在我的活动,我打电话从主要活动由 startActivityForResult
第二次活动。在我的第二个活动有一些完成本次活动(也许没有结果)一些方法,但只是其中之一返回结果。
In my activity, I'm calling a second activity from the main activity by startActivityForResult
. In my second activity there are some methods that finish this activity (maybe without result), however, just one of them return a result.
例如,从主要活动我所说的第二个。在此活动中,我检查手机的某些功能,比如它有一个摄像头。如果没有那我就关闭这个活动。此外,在MediaRecorder或MediaPlayer的的preparation如果问题发生然后我会关闭这个活动。
For example, from the main activity I call a second one. In this activity I'm checking some features of handset such as does it have a camera. If it doesn't have then I'll close this activity. Also, during preparation of MediaRecorder or MediaPlayer if a problem happens then I'll close this activity.
如果其设备有一个摄像头和录音是完全做得到,那么如果用户点击完成按钮然后我将结果发送(录制的视频地址)回主要活动录制视频后。
If its device has a camera and recording is done completely, then after recording a video if a user clicks on the done button then I'll send the result (address of the recorded video) back to main activity.
我如何检查结果从主要活动?
How do I check the result from the main activity?
从你的 FirstActivity
的 SecondActivity
使用来电 startActivityForResult()
方法
例如:
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
在你的 SecondActivity
设置要返回到 FirstActivity
中的数据。如果你不想返回,没有设置任何。
In your SecondActivity
set the data which you want to return back to FirstActivity
. If you don't want to return back, don't set any.
例如:在secondActivity如果要发回的数据:
For example: In secondActivity if you want to send back data:
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();
现在在你的FirstActivity类写入以下code为 onActivityResult()
方法。
Now in your FirstActivity class write following code for the onActivityResult()
method.
@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