1 简介
、如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int requestCode)方法打开新的Activity,新的Activity 关闭后会向前面的Activity传回数据,为了得到传回的数据,必须在前面的Activity中重写onActivityResult(int requestCode, int resultCode, Intent data)方法。
public class MainActivity extends Activity { private final static String TAG="MainActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnOpen=(Button)this.findViewById(R.id.btnOpen); btnOpen.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { //得到新打开Activity关闭后返回的数据 //第二个参数为请求码,可以根据业务需求自己编号 startActivityForResult(new Intent(MainActivity.this, OtherActivity.class), 1); } }); } /** * 为了得到传回的数据,必须在前面的Activity中(指MainActivity类)重写onActivityResult方法 * * requestCode 请求码,即调用startActivityForResult()传递过去的值 * resultCode 结果码,结果码用于标识返回数据来自哪个新Activity */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { String result = data.getExtras().getString("result");//得到新Activity 关闭后返回的数据 Log.i(TAG, result); } }
2 setResult
使用startActivityForResult(Intent intent, int requestCode)方法打开新的Activity,新Activity关闭前需要向前面的Activity返回数据需要使用系统提供的setResult(int resultCode, Intent data)方法实现:
public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); Button btnClose=(Button)findViewById(R.id.btnClose); btnClose.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { //数据是使用Intent返回 Intent intent = new Intent(); //把返回数据存入Intent intent.putExtra("result", "My name is linjiqin"); //设置返回数据 OtherActivity.this.setResult(RESULT_OK, intent); //关闭Activity OtherActivity.this.finish(); } }); } } setResult()方法的第一个参数值可以根据业务需要自己定义,上面代码中使用到的RESULT_OK是系统Activity类定义的一个常量,值为-1,代码片断如下: public class android.app.Activity extends ......{ public static final int RESULT_CANCELED = 0; public static final int RESULT_OK = -1; public static final int RESULT_FIRST_USER = 1; }
说明:当点击“打开新的Activity”按钮,会跳转到“我是新打开的Activity”页面;
当点击“关闭”按钮,关闭当前页面,同时跳转到“我是旧的Activity”页面,且会传递result参数给前一个Activity
3请求码的作用
使用startActivityForResult(Intent intent, int requestCode)方法打开新的Activity,我们需要为startActivityForResult()方法传入一个请求码(第二个参数)。请求码的值是根据业务需要由自已设定,用于标识请求来源。例如:一个Activity有两个按钮,点击这两个按钮都会打开同一个Activity,不管是那个按钮打开新Activity,当这个新Activity关闭后,系统都会调用前面Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法。在onActivityResult()方法如果需要知道新Activity是由那个按钮打开的,并且要做出相应的业务处理,这时可以这样做:
@Override public void onCreate(Bundle savedInstanceState) { .... button1.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { startActivityForResult (new Intent(MainActivity.this, NewActivity.class), 1); } }); button2.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { startActivityForResult (new Intent(MainActivity.this, NewActivity.class), 2); } }); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode){ case 1: //来自按钮1的请求,作相应业务处理 case 2: //来自按钮2的请求,作相应业务处理 } } }
4 结果码的作用
在一个Activity中,可能会使用startActivityForResult()方法打开多个不同的Activity处理不同的业务,当这些新Activity关闭后,系统都会调用前面Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法。为了知道返回的数据来自于哪个新Activity,在onActivityResult()方法中可以这样做(ResultActivity和NewActivity为要打开的新Activity):
public class ResultActivity extends Activity { ..... ResultActivity.this.setResult(1, intent); ResultActivity.this.finish(); } public class NewActivity extends Activity { ...... NewActivity.this.setResult(2, intent); NewActivity.this.finish(); } public class MainActivity extends Activity { // 在该Activity会打开ResultActivity和NewActivity @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(resultCode){ case 1: // ResultActivity的返回数据 case 2: // NewActivity的返回数据 } } }
5 Demo源码:
TestResultActivity.java
package mm.shandong.com.testresult; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class TestResultActivity extends AppCompatActivity { EditText editTextBrand; RadioGroup radioGroup; TextView textViewXH; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_result); editTextBrand = (EditText) findViewById(R.id.editTextBrand); radioGroup = (RadioGroup) findViewById(R.id.radioGroup); textViewXH = (TextView) findViewById(R.id.textViewXH); RadioButton radionButton = (RadioButton) radioGroup.getChildAt(0); radionButton.setChecked(true); } ///跳转到选择品牌界面 public void selectBrand(View view) { Intent intent = new Intent(this, TestResultActivity1.class); startActivityForResult(intent, 1); } ///选择要购买的电脑 public void selectCompute(View view) { Intent intent = new Intent(this, TestResultActivity2.class); String brand = editTextBrand.getText().toString(); RadioButton radionButton = (RadioButton) radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()); String nc = radionButton.getText().toString(); intent.putExtra("brand", brand); intent.putExtra("nc", nc); startActivityForResult(intent, 2); textViewXH.setText(""); } ///activity请求返回的回调 @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { switch (requestCode) { //resultCode为回传的标记 case 1: if (resultCode == 2) { String brand = intent.getStringExtra("brand"); editTextBrand.setText(brand); } break; case 2: if (resultCode == 3) { String xh = intent.getStringExtra("xh"); textViewXH.setText(xh); } break; } } }
TestResultActivity1
package mm.shandong.com.testresult; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class TestResultActivity1 extends AppCompatActivity { ListView listView; String[] brands = new String[]{"联想", "戴尔"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_result1); listView = (ListView) findViewById(R.id.listView); ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_checked, brands); listView.setAdapter(arrayAdapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } ///提交选择的品牌结果 public void submit(View view) { int index = listView.getCheckedItemPosition(); if (index < 0) { Toast.makeText(this, "请选择品牌", Toast.LENGTH_SHORT).show(); return; } String brand = (String) listView.getItemAtPosition(index); int resultCode = 2; Intent intent = getIntent(); intent.putExtra("brand", brand); setResult(2, intent); finish(); } }
TestResultActivity2
package mm.shandong.com.testresult; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class TestResultActivity2 extends AppCompatActivity { ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_result2); Intent intent=getIntent(); String brand=intent.getStringExtra("brand"); String nc=intent.getStringExtra("nc"); Map map=initData(); List<String> lists= (List<String>) map.get(brand+nc); listView= (ListView) findViewById(R.id.listView); ArrayAdapter arrayAdapter=new ArrayAdapter(this, android.R.layout.simple_list_item_checked,lists); listView.setAdapter(arrayAdapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } ///提交选择的电脑型号 public void submit(View view){ int index= listView.getCheckedItemPosition(); if(index<0){ Toast.makeText(this,"请选择型号",Toast.LENGTH_SHORT).show(); return ; } String xh= (String) listView.getItemAtPosition(index); int resultCode=2; Intent intent=getIntent(); intent.putExtra("xh",xh); setResult(3,intent); finish(); } ///初始化数据源 public Map initData(){ Map map=new HashMap(); List<String> lists=new ArrayList<>(); lists.add("联想1G0001"); lists.add("联想1G0002"); map.put("联想1G",lists); lists=new ArrayList<>(); lists.add("联想2G0001"); lists.add("联想2G0002"); map.put("联想2G",lists); lists=new ArrayList<>(); lists.add("戴尔1G0001"); lists.add("戴尔1G0002"); map.put("戴尔1G",lists); lists=new ArrayList<>(); lists.add("戴尔2G0001"); lists.add("戴尔2G0002"); map.put("戴尔2G",lists); return map; } }
本人微博:honey_11
Demo下载
最后,以上例子都来源与安卓无忧,请去应用宝或者豌豆荚下载:例子源码,源码例子文档一网打尽