废话不多说了,直接给大家贴代码了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package com.example.testactivityresquest; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.toast; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button btn = (button) findviewbyid(r.id.button); btn.setonclicklistener( new onclicklistener() { @override public void onclick(view v) { intent intent = new intent(mainactivity. this , activityb. class ); int [] nums = { , }; intent.putextra(changliang.key, nums); //有别于startactivity,如果启动的其他activity多了以后。相当于定一个特定key值,返回根据key值返回。 startactivityforresult(intent, changliang.requestcode); } }); } //activityb传回来的数据在这个方法中获取 @override protected void onactivityresult( int requestcode, int resultcode, intent data) { int s = data.getintextra(changliang.activity_b_key, ); toast.maketext(getapplicationcontext(), "传递两个数得到的和是:" + s, ).show(); } } package com.example.testactivityresquest; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.toast; public class activityb extends activity { @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activityb); intent intent = this .getintent(); int [] n = intent.getintarrayextra(changliang.key); final int nums = n[] + n[]; toast.maketext( this , n[] + " " + n[], ).show(); button btn = (button) findviewbyid(r.id.button); btn.setonclicklistener( new onclicklistener() { @override public void onclick(view v) { intent intent = new intent(activityb. this , mainactivity. class ); intent.putextra(changliang.activity_b_key, nums); // 将数据根据特定键值的意图事件导入 activityb. this .setresult(changliang.requestcode, intent); //关闭后返回主activity activityb. this .finish(); } }); } } package com.example.testactivityresquest; public class changliang { public static final string key= "key" ; public static final string activity_b_key= "key1" ; public static final int requestcode= 1987 ; } |
xml文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "#ff" > <button android:id= "@+id/button" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true" android:layout_margintop= "dp" android:text= "启动activityb" /> </relativelayout> <?xml version= "." encoding= "utf-" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "#ff" android:orientation= "vertical" > <button android:id= "@+id/button" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "返回activity" /> </linearlayout> |
别忘在androidmanifast中注册activityb。
运行效果图:
startactivityforresult与startactivity的不同之处在于:
1、startactivity( )
仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startactivity( )。
2、startactivityforresult( )
可以一次性完成这项任务,当程序执行到这段代码的时候,假若从t1activity跳转到下一个text2activity,而当这个text2activity调用了finish()方法以后,程序会自动跳转回t1activity,并调用前一个t1activity中的onactivityresult( )方法。