如何将意图中的额外内容传递给onActivityResult()方法?

时间:2017-08-13 14:29:14

标签: android android-intent

我试图将一些数据从意图传递给onActivityResult()方法,这就是我所做的:

Item 0
process is called
process is called
Item 1
process is called
Item 2
process is called
Item 3
process is called...

Desired output:
Item 0
process is called
process is called
process is called
process is called
process is called
process is called
Item 1

然后:

Intent intent = new Intent();
intent.putExtra("STRING", some_data);
startActivityForResult(intent, 1);

但是这给了我一个NullPointerException(data == null),我不明白,我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

我们假设您使用Activity

Activity A开始startActivityForResult(intent, 1); B

您在此意图中传递的数据可在Activity B

中找到

如果您想将某些数据从Activity B传回Activity A,则必须在调用活动B中的setResult()之前致电finish()。如下所示:

        Intent intent = new Intent();
        intent.putExtra("STRING", some_data);
        setResult(Activity.RESULT_OK, intent);
        finish();

有关详细信息,请参阅here

答案 1 :(得分:0)

你可以打电话

setResult(int resultCode, Intent data)

在第二次活动的call finish()方法之前。

https://developer.android.com/reference/android/app/Activity.html#setResult(int, android.content.Intent)