我正在调用FirstActivity对SecondActivity的意图。我使用requestCode和Intent调用startActivityForResult,但在SecondActivity中,setResult调用SecondActivity而不是调用FistActivity。这是我的代码
FirstActivity
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
startActivityForResult(i,1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getApplicationContext(),"result"+data.getStringExtra("data"),Toast.LENGTH_SHORT).show();
}
SecondActivity
Intent i=getIntent();
i.putExtra("data","hi");
setResult(1,i);
finish();
清单
<activity
android:name=".FirstActivity"
android:windowSoftInputMode="adjustPan|stateAlwaysHidden"
android:theme="@style/MyMaterialTheme"
android:screenOrientation="portrait">
<intent-filter>
<data android:scheme="example"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:theme="@style/MyMaterialTheme"
android:windowSoftInputMode="adjustPan|stateAlwaysHidden"
android:screenOrientation="portrait">
</activity>
答案 0 :(得分:4)
更改自:
Intent i=getIntent();
为:
Intent i=new Intent();// change this line
答案 1 :(得分:1)
传递
setResult(Activity.RESULT_OK, i);
而不是
setResult(1,i);
答案 2 :(得分:0)
你必须通过拥有一个新的意图目标并最后完成当前活动来实现它。
Intent resultIntent = new Intent();
resultIntent.putExtra("data","hi");
setResult(Activity.RESULT_OK, resultIntent);
finish();
希望这有帮助!