When starting another activity, you can use either startactivity or startactivityforresult directly. I'm sure everyone knows how to use the previous one. This article mainly uses a demo to learn the second one.
Startactivityforresult is mainly used to return data. Assume that we have two pages. First, we enter the first page with a button in it to enter the next page. When we enter the next page, after the finish () or back action, return the set value to the first page to display the value on the first page. This is like the callback method. After the finish () action or back action on the second page, the onactivityresult () method on the first page is called back. Therefore, we can rewrite this method. Check the Code directly:
Code for the first page:
Package Org. sunchao; 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. textview; public class teststartactivityforresultactivity extends activity implementsonclicklistener {private textview mtext01; private textview mtext02; private button button01; private button button02; private intent mintent; private int requestcode; /** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mtext01 = (textview) findviewbyid (R. id. mtext01); mtext02 = (textview) findviewbyid (R. id. mtext02); button01 = (button) findviewbyid (R. id. mbutton01); button02 = (button) findviewbyid (R. id. mbutton02); button01.setonclicklistener (this); button02.setonclicklistener (this); mtext01.settext ("01"); mtext02.settext ("02"); mintent = new intent (); mintent. setclass (teststartactivityforresultactivity. this, activity02.class) ;}@ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. mbutton01: // The value of the Request Code can be set at will, but must be> = 0 requestcode = 0; startactivityforresult (mintent, requestcode); break; case R. id. mbutton02: requestcode = 2; startactivityforresult (mintent, requestcode); break; default: Break ;}// callback method, when you return from the second page, this method @ overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {string change01 = data. getstringextra ("change01"); string change02 = data. getstringextra ("change02"); // is the difference between switch (requestcode) {Case 0: mtext01.settext (change01); break; Case 2: mtext02.settext (change02); break; default: Break ;}}}
The first page layout file:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: id = "@ + ID/mtext01" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <textview Android: Id = "@ + ID/mtext02" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <button Android: Id = "@ + ID/mbutton01" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "change the value of the first line of text"/> <button Android: Id = "@ + ID/mbutton02" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "change the value of the second line of text"/> </linearlayout>
The second page code:
Package Org. sunchao; import android. app. activity; import android. content. intent; import android. OS. bundle; public class activity02 extends activity {private int resultcode = 0; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity02); intent mintent = new intent (); mintent. putextra ("change01", "1000"); mintent. putextra ("change02", "2000"); // set the result and send this. setresult (resultcode, mintent); // This. finish ();}}
The second page layout file:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "The text value has changed"/> </linearlayout>
Androidmanifest. xml:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.sunchao" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TestStartActivityForResultActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Activity02" /> </application></manifest>
Run:
Code: http://download.csdn.net/source/3448804