Passing data between Activty
Prior to learning how to start an activity, in some cases not only need to start another activity, but also to get the results returned. And sometimes more often when you start another activity with some values.
1. Define a intent and specify the activity that will be started for the intent
2. Call Startactivityforresult (Intent intent,int Requestcode) to jump and Mark Requestcode
3. Rewrite onactivityresult (int requestcode,int resultcode,intent data) to parse the returned
4. Call Setresult (result_ok,backintent) in another activity
Note: Data is passed between activity
Bundle: A type-safe container that can only store arrays of basic data types or basic types, such as String,int,byte and Boolean
Code:
Activity_main.xml:
<Relativelayoutxmlns: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"Tools:context= "${relativepackage}.${activityclass}" > <ButtonAndroid:text= "Next"Android:id= "@+id/next"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center_horizontal" > </Button></Relativelayout>
Mainactivity.java:
Packagecom. Yzr.activtydatapass;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {Final intnext=0x1010; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button btn=(Button) Findviewbyid (R.id.next); Btn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubIntent nextintent=NewIntent (mainactivity. This, Nextactivity.class); Bundle Bundle=NewBundle (); Bundle.putstring ("Gender", "man"); Bundle.putstring ("Name", "YZR"); Nextintent.putextras (bundle); Startactivityforresult (Nextintent, NEXT); } }); } @Overrideprotected voidOnactivityresult (intRequestcode,intresultcode,intent data) { if(Resultcode==result_ok && requestcode==NEXT) {Bundle Bundle=Data.getextras (); intAge=bundle.getint ("Age"); String Work=bundle.getstring ("occupation"); Settitle ("Age:" +age+ ", Work:" +Work ); } Super. Onactivityresult (Requestcode, ResultCode, data); }}
Activity_next.xml:
<Relativelayoutxmlns: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"Tools:context= "${relativepackage}.${activityclass}" > <ButtonAndroid:text= "Back"Android:id= "@+id/back"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center_horizontal" > </Button></Relativelayout>
Nextactivity.java:
Packagecom. Yzr.activtydatapass;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classNextactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_next); Bundle Bundle=getintent (). Getextras (); String name=bundle.getstring ("name"); String Sex=bundle.getstring ("Gender"); Settitle ("Name:" +name+ ", Gender:" +Sex); Button btn=(Button) Findviewbyid (r.id.back); Btn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubIntent backintent=NewIntent (); Bundle Bundle=NewBundle (); Bundle.putint ("Age", 25); Bundle.putstring ("Occupation", "IT"); Backintent.putextras (bundle); Setresult (result_ok,backintent); Finish (); } }); }}
Androidmanifest.xml:
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "com. Yzr.activtydatapass "Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" /> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ActivityAndroid:name=". Nextactivity "Android:label= "@string/title_activity_next" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.VIEW" /> <categoryAndroid:name= "Android.intent.category.DEFAULT" /> </Intent-filter> </Activity> </Application></Manifest>
The above is only for learning <android System Application Development actual combat detailed >
END
Android Note: Activity's band value transfer