如何在 Android 上管理 startActivityForResult [英] How to manage startActivityForResult on Android

查看:31
本文介绍了如何在 Android 上管理 startActivityForResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动中,我通过 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 a result), however, just one of them returns a result.

例如,从主要活动中,我调用了第二个活动.在本次活动中,我将检查手机的某些功能,例如它是否有摄像头.如果没有,那么我将关闭此活动.此外,在MediaRecorderMediaPlayer 的准备过程中,如果出现问题,我将关闭此活动.

For example, from the main activity, I call a second one. In this activity, I'm checking some features of a handset, such as does it have a camera. If it doesn't have then I'll close this activity. Also, during the 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 the main activity.

如何查看主要活动的结果?

How do I check the result from the main activity?

推荐答案

从您的 FirstActivity,使用 startActivityForResult() 调用 SecondActivity方法.

From your FirstActivity, call the SecondActivity using the startActivityForResult() method.

例如:

int LAUNCH_SECOND_ACTIVITY = 1
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, LAUNCH_SECOND_ACTIVITY);

在你的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();

如果您不想返回数据:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();

现在在您的 FirstActivity 类中,为 onActivityResult() 方法编写以下代码.

Now in your FirstActivity class, write the following code for the onActivityResult() method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == LAUNCH_SECOND_ACTIVITY) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            // Write your code if there's no result
        }
    }
} //onActivityResult

要在 Kotlin 中以更好的方式在两个 Activity 之间传递数据,请通过 '一种在活动之间传递数据的更好方法'.

To implement passing data between two activities in a much better way in Kotlin, please go through 'A better way to pass data between Activities'.

这篇关于如何在 Android 上管理 startActivityForResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆