Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

android - 如何在Android上管理startActivityForResult?(How to manage startActivityForResult on Android?)

In my activity, I'm calling a second activity from the main activity by startActivityForResult .

(在我的活动中,我正在通过startActivityForResult从主活动中调用第二个活动。)

In my second activity there are some methods that finish this activity (maybe without result), however, just one of them return a result.

(在我的第二个活动中,有一些方法可以完成此活动(可能没有结果),但是只有其中一个返回结果。)

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.

(另外,在准备MediaRecorderMediaPlayer如果发生问题,那么我将关闭此活动。)

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?

(如何检查主要活动的结果?)

  ask by Hesam translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...