启动Activity的两种方式startActivity和startActivityForResult(二)

最后更新于:2022-04-01 16:14:41

startActivityForResult用于启动其他指定的Activity,而且期望获取该指定Activity返回的结果。 启动其他Activity并返回结果过程: 当前ActivityA需要调用startActivityForResult(int requestCode,Intent intent)请求调用参数intent对应的ActivityB,请求码是requestCode,同时需要重写onActivityResult(int requestCode,int resultCode,Intent intent),当被启动的ActivityB返回结果时,必须使用setResult(int resultCode,Intent intent)将携带数据的intent传回到ActivityA,这个传回动作响应的结果码是resultCode,此时会触发onActivityResult,然后请求码和结果码就唯一确定一个过程,然后就可以接下来处理了 请求码和结果码 一个Activity中可能包含多个按钮,并调用多个startActivityForResult方法来打开不同的Activity处理业务,当这些新Activity关闭后,系统会调用前面的Activity的onActivityResult方法。为了知道该方法是由哪个请求结果所触发的,可利用requestCode标识,为了知道返回的数据来自于那个新的Activity,可利用resultCode标识 例,第二个Activity让用户选择信息然后把结果返回给第一个Activity MainActivity.java ~~~ package com.hust.activityforresult; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { Button bn; EditText edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bn=(Button) findViewById(R.id.button1); edit=(EditText) findViewById(R.id.editText1); bn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub //创建需要对应目标Activity的intent Intent intent=new Intent(MainActivity.this,CityActivity.class); //启动指定Activity并等待返回的结果,0是请求码。用于表示该请求 startActivityForResult(intent ,0); } }); } //重写该方法,以回调的形式来获取Activity返回的结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); //当请求码和结果码都是0时,也就是处理的特定结果 if(requestCode==0&&resultCode==0){ //获取Bundle中的数据 Bundle bundle=data.getExtras(); String city=bundle.getString("city"); //修改编辑框的内容 edit.setText(city); } } ~~~ CityActivity.java ~~~ package com.hust.activityforresult; import android.app.ExpandableListActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; 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.ExpandableListView.OnChildClickListener; import android.widget.TextView; public class CityActivity extends ExpandableListActivity { //定义省份 private String[] provinces={ "广东","湖北","河南" }; //定义城市 private String[][] cities={ {"广州","深圳","珠海","惠州"}, {"武汉","襄阳","随州","十堰"}, {"郑州","平顶山","信阳","驻马店"} }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_city); ExpandableListAdapter ad=new BaseExpandableListAdapter(){ private TextView getTextView(){ AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64); TextView textview =new TextView(CityActivity.this); textview.setLayoutParams(lp); textview.setTextSize(16); textview.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT); textview.setPadding(30, 5, 0, 5); return textview; } @Override public int getGroupCount() { // TODO Auto-generated method stub return provinces.length; } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return cities[groupPosition].length; } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return provinces[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return cities[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub TextView text=getTextView(); text.setTextColor(Color.RED); text.setText(getGroup(groupPosition).toString()); return text; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub TextView text=getTextView(); text.setText(getChild(groupPosition, childPosition) .toString()); return text; } //主要在给子选项设置监听器时,这个函数一定要返回true,否则监听器没有用 @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } }; //设置adapter setListAdapter(ad); //为每个子选项添加监听器 getExpandableListView().setOnChildClickListener(new OnChildClickListener(){ @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // TODO Auto-generated method stub //获取启动该Activity之前的Activity对应的Intent Intent intent=getIntent(); intent.putExtra("city", cities[groupPosition][childPosition]); //设置该Activity的结果码,并设置结束之后退回的Activity CityActivity.this.setResult(0, intent); //结束本Activity CityActivity.this.finish(); return false; } }); } ~~~ ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9b037ede.jpg) ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9b04d634.jpg) ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9b06135b.jpg)
';