Activity提供了一个startActivityForResult(Intent intent, int requestCode)方法来启动其他Activity。该方法用于启动指定Activity,而且期望获取指定Activity返回的结果。这种请求对于实际应用也是很常见的,例如应用程序第一个界面需要用户进行选择--但需要选择的列表数据比较复杂,必须启动另一个Activity让用户选择。当用户在第二个Activity选择完成后,程序返回第一个Activity,第一个Activity必须能获取并显示用户在第二个Activity选择的结果。在这种应用场景下,也是通过Bundle进行数据交换的。
为了获取被启动的Activity所返回的结果,当前Activity需要重写onActivityResult(int requestCode,int resultCode, Intent intent),其中requestCode代表请求码,而resultCode代表Activity返回的结果码,这个结果码也是由开发者根据业务自行设定的。
一个Activity中可能包含多个按钮,并调用多个startActivityForResult()方法来打开多个不同的Activity处理不同的业务,当这些新的Activity关闭后,系统都会调用前面Activity的onActivityResult(int requestCode,int resultCode,Intent data)方法。为了知道该方法是由哪个请求的结果所触发的,可利用requestCode请求码,为了知道返回的数据来自于哪个新的Activity,可利用resultCode结果码。
下面通过一个示例来介绍如何启动Activity并获取被启动Activity的结果。
第一个Activity(ActivityForResult):
布局文件(activity_activity_for_result.xml)的代码如下:
ActivityForResult.java的代码如下:
另一个Activity不需要布局文件,直接用代码搞定,SelectCityActivity.java的代码如下:
public classSelectCityActivityextendsExpandableListActivity {
//定义省份数组
privateString[]provinces=newString[]{
"广东","广西","湖南"
};
privateString[][]cities=newString[][]
{
{"广州","深圳","珠海","中山"},
{"桂林","柳州","南宁","北海"},
{"长沙","岳阳","衡阳","株洲"}
};
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListAdapter adapter =newBaseExpandableListAdapter() {
@Override
public intgetGroupCount() {
returnprovinces.length;
}
@Override
public intgetChildrenCount(intgroupPosition) {
returncities[groupPosition].length;
}
@Override
publicObject getGroup(intgroupPosition) {
returnprovinces[groupPosition];
}
@Override
publicObject getChild(intgroupPosition,intchildPosition) {
returncities[groupPosition][childPosition];
}
@Override
public longgetGroupId(intgroupPosition) {
returngroupPosition;
}
@Override
public longgetChildId(intgroupPosition,intchildPosition) {
returnchildPosition;
}
@Override
public booleanhasStableIds() {
return true;
}
@Override
publicView getGroupView(intgroupPosition,booleanisExpanded, View convertView, ViewGroup parent) {
LinearLayout ll =newLinearLayout(SelectCityActivity.this);
ll.setOrientation(0);
ImageView logo =newImageView(SelectCityActivity.this);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
returnll;
}
privateTextView getTextView() {
AbsListView.LayoutParams lp =newAbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,64);
TextView textView =newTextView(SelectCityActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(36,0,0,0);
textView.setTextSize(20);
returntextView;
}
@Override
publicView getChildView(intgroupPosition,intchildPosition,booleanisLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
returntextView;
}
@Override
public booleanisChildSelectable(intgroupPosition,intchildPosition) {
return true;
}
};
//设置该窗口显示列表
setListAdapter(adapter);
getExpandableListView().setOnChildClickListener(newExpandableListView.OnChildClickListener() {
@Override
public booleanonChildClick(ExpandableListView parent, View v,intgroupPosition,intchildPosition,longid) {
//获取启动该Activity之前的Activity对应的Intent
Intent intent = getIntent();
Bundle data =newBundle();
data.putString("city",cities[groupPosition][childPosition]);
intent.putExtras(data);
SelectCityActivity.this.setResult(0, intent);
SelectCityActivity.this.finish();
return false;
}
});
}
}
运行结果: