2 回答
TA贡献1853条经验 获得超6个赞
1-首先,您必须使用片段从您的 Activity 启动startActivityForResult()
2-确保调用super.onActivityResult
您的活动onActivityResult()
方法(如果您正在实施它)
3-在您的片段中,您只需要根据需要实现onActivityResult()
和处理结果
TA贡献1836条经验 获得超3个赞
要将数据从您的活动中获取到您的意图,请将其添加为:
Intent intent = new Intent(this, MyIntent.class);
intent.putExtra("aString", aString);
intent.putExtra("aSomething", customSerializableType);
startActivityForResult(intent, MY_REQUEST_ID);
在意图中得到它:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Intent intent = getIntent();
String newString = intent.getStringExtra("aString");
CustomSerializableType newSomething = intent.getSerializableExtra("aSomething");
}
根据您的意图发回数据:
private void done() {
Intent intent = new Intent();
intent.putExtra("aString", aString);
intent.putExtra("aSomething", customSerializableType);
setResult(Activity.RESULT_OK, intent);
finish();
}
并在托管活动中获取这些数据
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case MY_REQUEST_ID:
if (resultCode == Activity.RESULT_OK) {
String newString = data.getStringExtra("aString");
CustomSerializableType newSomething = data.getSerializableExtra("aSomething");
}
break;
}
添加回答
举报