Miscellaneous: Returning data from an activity

Source: Internet
Author: User

The first activity code:

Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. textview; </P> <p> public class forresultactivity extends activity {<br/> private textview; <br/> @ override <br/> Public voi D oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> textview = (textview) findviewbyid (R. id. textview2); <br/> button = (button) findviewbyid (R. id. button12); </P> <p> button. setonclicklistener (New onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> Run (); </P> <p >}< br/>}); </P> <p >}</P> <p> private void run () {<Br/> intent = new intent (); <br/> intent. putextra ("name", "chenzheng_java"); <br/> intent. putextra ("Age", 23); <br/> intent. putextra ("isman", true); <br/> intent. putextra ("Description", "give you a handsome guy"); <br/> intent. setclass (forresultactivity. this, returnresultactivity. class); <br/> // you can use this method to obtain the data returned by the subsequent activity (mainly through intent interaction) <br/> startactivityforresult (intent, 1 ); <br/>}</P> <p>/*** <br/> * after obtaining This method will be rewritten for the content returned by the edge activity. This method will be triggered when the subsequent activity calls the setresult method. <Br/> * requestcode Request Code <br/> * That is, the 1 we specify in startactivityforresult. We can use this identifier to determine, whether the returned result from the following activity is what we requested <br/> * The result code returned by the resultcode is the value of the activity that gives us an identifier status information <br/> * The intent object returned by data <br /> */<br/> @ override <br/> protected void onactivityresult (INT requestcode, int resultcode, intent data) {<br/> switch (resultcode) {<br/> case result_ OK: <br/> bundle = data. getextras (); <br/> string info = bundle. getstring ("info"); <br/> textview. settext (Info + "requestcode =" + requestcode); <br/> break; </P> <p> default: <br/> break; <br/>}</P> <p>}

The second activity code:

Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. edittext; </P> <p> public class returnresultactivity extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstanc Estate); <br/> setcontentview (R. layout. layout1); <br/> button = (button) findviewbyid (R. id. button); <br/> button. setonclicklistener (new view. onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> intent intent2 = new intent (); <br/> intent2.putextra ("info", "Hahahaha"); <br/> // call the setresult method to return the result to the previous activity. <Br/> returnresultactivity. this. setresult (result_ OK, intent2); <br/> // close the current activity <br/> returnresultactivity. this. finish (); <br/>}); <br/>}< br/>

Remember to declare two activities in the androidmanifest. xml file. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "cn.com. chenzheng_java "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <uses-SDK Android: minsdkversion = "8"/> </P> <p> <application Android: icon = "@ drawable/icon" Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". forresultactivity "<br/> Android: Label =" @ string/app_name "> <br/> <Intent-filter> <br/> <action Android: Name = "android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <! -- Declare the activity here, otherwise the system cannot find the activity --> <br/> <activity Android: name = "returnresultactivity"> </activity> <br/> </Application> <br/> </manifest>

-------------------------------------------------------------------------

Let's sum up the following questions about inter-activity jump:

The jump between the activity and the activity is performed through the intent.

Intent has a setclass method, intent setclass (context packagecontext, class <?> CLs) the first parameter is the current context, and the second parameter is the activity to jump.

You can use the startactivity () method to perform the jump. No value is returned for this jump. If you want the following activity to return some data to the current activity, you can only use the startactivityforresult () method. Void startactivityforresult (intent, int requestcode). The first parameter is intent, and the second parameter is a request code. You have set this number.

In addition, to obtain the data returned by the following activity, we need to implement the onactivityresult (INT requestcode, int resultcode, intent data) method in the current activity, the requestcode is consistent with the requestcode in the startactivityforresult method. (Requestcode is actually very useful. If our current activity can jump to many activities and each activity will return data to you, how can we determine which request is returned, here is the requestcode used ).

In the following activity (the activity we enable through startactivityforresult), we use this. setresult () method to return data. The first parameter of setresult (INT resultcode, intent data) is the identifier, which is the resultcode of the onactivityresult method implemented by the previous activity, we can use this value to determine whether data processing is successful.

Possible value: result_canceled result_ OK result_first_user.

Call the finish method after setresult () to destroy the current activity and give the previous activity a display opportunity.

In addition, you can also use intent. the setclassname (string packagename, string classname) method sets the jump target. packagename indicates the package name of the current activity and classname indicates the full path name of the target activity!

Of course, the setclassname (context packagecontext, string classname) of intent is also the same function. Their API section is attached here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.