Activity实例详解之启动activity并返回结果
先给大家展示下效果展示图:
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); } }
当新activity关闭后,新activity返回的数据通过intent进行传递,android平台会调用前面activity 的onactivityresult()方法,把存放了返回数据的intent作为第三个输入参数传入,在onactivityresult()方法中使用第三个输入参数可以取出新activity返回的数据。
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; } }
以上所述是小编给大家介绍的activity实例详解之启动activity并返回结果,希望对大家有所帮助
推荐阅读
-
Activity实例详解之启动activity并返回结果
-
Activity配置、启动和关闭activity实例详解
-
Activity实例详解之启动activity并返回结果
-
Android Activity启动模式之singleTask实例详解
-
Android Activity启动模式之standard实例详解
-
Android Activity启动模式之singleTop实例详解
-
Android Activity启动模式之singleTop实例详解
-
Android Activity启动模式之standard实例详解
-
Android Activity启动模式之singleTask实例详解
-
详解Activity之singletast启动模式及如何使用intent传值