在Android的Activity传递中,有时候因项目需要,不仅需要从当前Activity切换到另一个Activity,还需要从另一个Activity获取返回的数据,供当前Activity使用。
本例包含两个Activity,第一个Activity只有一个Button和TextView,Button用于切换到另一个Activity选择城市,TextView用于显示结果;另一个Activity使用的是ExpandableListActivity(可展开列表),用于显示供选择的城市。
1.MainActivity:
package com.example.activityforresult; import android.content.Intent; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity {public Button select; public TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); select = findViewById(R.id.select); show = findViewById(R.id.show); //绑定监听器 select.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //创建需要对应于目标Activity的Intent Intent intent = new Intent(MainActivity.this , SelectCityActivity.class); //启动指定Activity并等待返回结果,其中0是请求码,用于标识该请求 startActivityForResult(intent , 0); } }); }@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) { //当requestCode、resultCode同为0时,处理待定的结果 if (requestCode == 0 && resultCode == 0){ //取出Intent中的Extras数据 Bundle data = https://www.it610.com/article/intent.getExtras(); //取出Bundle中的数据 String resultCity = data.getString("city"); //将获得的数据显示到文本框中 show.setText(resultCity); } } }
【Android|Android Activity之启动其他Activity并返回结果】2.SelectCityActivity:
package com.example.activityforresult; import android.app.ExpandableListActivity; import android.content.Intent; import android.os.Bundle; import android.os.PersistableBundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class SelectCityActivity extends ExpandableListActivity { //定义省份数组 private String[] provinces = new String[]{ "广东" , "广西" , "湖南" }; //定义城市数组 private String[][] cities = new String[][]{ {"广州" , "深圳" , "珠海" , "中山"} , {"桂林" , "柳州" , "南宁" , "北海"} , {"长沙" , "岳阳" , "衡阳" , "株洲"} }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ExpandableListAdapter adapter = new BaseExpandableListAdapter() { private TextView getTextView(){ AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT , 64); TextView textView = new TextView(SelectCityActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(36 , 0, 0 , 0); textView.setTextSize(20); return textView; } //获取指定组位置、指定子列表项处的子列表数据 @Override public int getGroupCount() { return provinces.length; }@Override public int getChildrenCount(int groupPosition) { return cities[groupPosition].length; }@Override public Object getGroup(int groupPosition) { return provinces[groupPosition]; }@Override public Object getChild(int groupPosition, int childPosition) { return cities[groupPosition][childPosition]; }@Override public long getGroupId(int groupPosition) { return groupPosition; }@Override public long getChildId(int groupPosition, int childPosition) { return childPosition; }@Override public boolean hasStableIds() { return true; } //该方法决定每个组选项的外观 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(SelectCityActivity.this); ll.setOrientation(LinearLayout.HORIZONTAL); ImageView logo = new ImageView(SelectCityActivity.this); ll.addView(logo); TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } //该方法决定每个子选项的外观 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView(); textView.setText(getChild(groupPosition , childPosition).toString()); return textView; }@Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }; setListAdapter(adapter); getExpandableListView().setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { //获取启动该Activity之前的Activity对应的Intent Intent intent = getIntent(); intent.putExtra("city" , cities[groupPosition][childPosition]); //设置结果码,并设置结束之后退回的Activity SelectCityActivity.this.setResult(0 , intent); SelectCityActivity.this.finish(); return false; } }); } }
3.MainActivity的XML布局文件 activity_main.xml(SelectCityActivity无XML,布局用java代码实现):
4.前面由于有两个Activity:MainActivity和SelectCityActivity,其中MainActivity在创建时会自动声明,SelectCityActivity需要在AndroidManifest.xml中自己声明,不然程序会报错: