如何在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? 如何检查主要活动的结果?


#1楼

参考:https://stackoom.com/question/hfNP/如何在Android上管理startActivityForResult


#2楼

How to check the result from the main activity? 如何检查主要活动的结果?

You need to override Activity.onActivityResult() then check its parameters: 您需要覆盖Activity.onActivityResult()然后检查其参数:

  • requestCode identifies which app returned these results. requestCode标识哪个应用返回了这些结果。 This is defined by you when you call startActivityForResult() . 这是由您在调用startActivityForResult()时定义的。
  • resultCode informs you whether this app succeeded, failed, or something different resultCode通知您此应用是否成功,失败或其他不同
  • data holds any information returned by this app. data包含此应用返回的所有信息。 This may be null . 这可能为null

#3楼

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

#4楼

If you want to update the user interface with activity result, you can't to use this.runOnUiThread(new Runnable() {} Doing this the UI won't refresh with new value. Instead, you can do this: 如果要使用活动结果更新用户界面,则不能使用this.runOnUiThread(new Runnable() {}这样做UI不会刷新为新值,而是可以执行以下操作:

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

    if (resultCode == RESULT_CANCELED) {
        return;
    }

    global_lat = data.getDoubleExtra("LATITUDE", 0);
    global_lng = data.getDoubleExtra("LONGITUDE", 0);
    new_latlng = true;
}

@Override
protected void onResume() {
    super.onResume();

    if(new_latlng)
    {
        PhysicalTagProperties.this.setLocation(global_lat, global_lng);
        new_latlng=false;
    }
}

This seems silly but works pretty well. 这似乎很愚蠢,但效果很好。


#5楼

首先,您将startActivityForResult()与第一个Activity参数一起使用,如果要将数据从第二个Activity发送到第一个Activity然后使用IntentsetResult()方法传递值,并在第一个Activity onActivityResult()方法中获取该数据。


#6楼

Complementing the answer from @Nishant,the best way to return the activity result is: 补充@Nishant的答案,返回活动结果的最佳方法是:

Intent returnIntent = getIntent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();

I was having problem with 我有问题

new Intent();

Then I found out that the correct way is using 然后我发现正确的方法是使用

getIntent();

to get the current intent 得到当前的意图

发布了0 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105326560
今日推荐