Android Learning Series (v) data transfer between activity

Source: Internet
Author: User

Preface: We have mastered the basic use of activity in the first place, but that is done in the same activity. So how do we do this if we want to interact with data between different Activity? So now we're still using the example of a demo login to illustrate

I. Description of requirements:    impersonate the user login registration behavior, the user registration information, the registration of the account, password this data to the login interface. Second, the code implementation:1. Layout File code:

    • Activity_main.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <edittext android:id= "@+id/et_uname" android:layout_width= "Fill_parent" Android:layout_hei ght= "Wrap_content" android:hint= "@string/et_uname"/> <edittext android:id= "@+id/et_upass" a     Ndroid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:hint= "@string/et_upass"/> <linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android: orientation= "Horizontal" > <button android:layout_width= "0DP" android:layout_height= "WR Ap_content "android:layout_weight=" 1 "android:onclick=" Login "android:text=" @string/bt_lo       Gin "/> <button     Android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:onclick= "Regist" android:text= "@string/bt_regist"/> </linearlayout></linearlayout >

    • Activity_regist.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <edittext        android:id= "@+id/et_uname" android:layout_width= "Fill_        Parent "        android:layout_height=" wrap_content "        android:hint=" @string/et_uname "/>    <edittext        android:id= "@+id/et_upass"        android:layout_width= "fill_parent"        android:layout_height= "Wrap_ Content "        android:hint=" @string/et_upass "/>    <button        android:layout_width=" Fill_parent "        android:layout_height= "wrap_content"        android:onclick= "regist"        android:text= "@string/bt_regist" /></linearlayout>
2.java file Code:
    • mainactivity.java:
public class Mainactivity extends Activity {private EditText et_uname;private EditText et_upass; @Overrideprotected void O Ncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initviews ();} private void Initviews () {et_uname = (EditText) Findviewbyid (r.id.et_uname); et_upass = (EditText) Findviewbyid (r.id.et_ UPass);} /** * Login * @param view */public void Login (view view) {String uname = Et_uname.gettext (). toString (). Trim (); String UPass = Et_upass.gettext (). toString (). Trim (); if (Textutils.isempty (uname) | | Textutils.isempty (UPass)) {Toast.maketext (this, "username password cannot be empty! ", Toast.length_short). Show ();} else {Toast.maketext (this, "Login successful! ", Toast.length_short). Show ();}} /** * Registration * @param view */public void regist (view view) {Intent Intent = new Intent (this, registactivity.class); int REQUESTC Ode = 0;//The request code to set the registration operation is 0startActivityForResult (intent, requestcode);} /** * method to invoke when the newly opened Activity is closed: In this function we do the data processing * For example we get the data returned from Registactivity * * @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (Requestcode, ResultCode, data); System.out.println ("Onactivityresult ..."); if (data! = NULL) {if (Requestcode = = 0) {String uname = Data.getstringextra (" Uname "); String UPass = Data.getstringextra ("UPass"); Et_uname.settext (uname); Et_upass.settext (UPass);}}}
Code Annotations :1) Login (view view), Regist (view view): This is the button click event, about the button click event, please refer to my other blog post: http://blog.csdn.net/gulu_gulu_jp/ article/details/468764372) regist () function: because of the different requirements, if we still use StartActivity (Intent Intent) This API is not enough to meet our needs, we need to use the new API, that is Startactivi Tyforresult (Intent Intent, int requestcode). The API allows us to open an activity and accept the value passed by the new activity when the newly opened activity is closed3) The function of the request code (REQUESTCODE): Mainly used to differentiate the source of the operation, if there are two operations in the current activity to open a new activity and get its return value, then we can set different request code for different operations, Onactivityresult the operation according to the request code to encode the corresponding business 4) Onactivityresult (): We can process the data returned by the open Activity in this function
    • Registactivity.java:
public class Registactivity extends Activity{private EditText et_uname;private EditText et_upass; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ regist); Initviews ();} private void Initviews () {et_uname = (EditText) Findviewbyid (r.id.et_uname); et_upass = (EditText) Findviewbyid (r.id.et_ UPass);} public void regist (view view) {String uname = Et_uname.gettext (). toString (). Trim (); String UPass = Et_upass.gettext (). toString (). Trim (); if (Textutils.isempty (uname) | | Textutils.isempty (UPass)) {Toast.maketext (this, "username password cannot be empty! ", Toast.length_short). Show ();} else {Toast.maketext (this, "registered successfully! Toast.length_short). Show (); int resultcode = 0;//return code: Similar to the request code, used to differentiate the returned result (when multiple return values)/* * Prepare the returned data: Intent is a */intent that can pass data data = new Intent ();d Ata.putextra ("uname", uname);d Ata.putextra ("UPass", UPass); Setresult (ResultCode, data);// Returns the result of finish ();//close Current Activity: pass data to the caller of the current activity, the caller will execute the Onactivityresult method}}}
3. Operation Result:
Third, the code implementation of the second method: Intent flexible use Look back at the code you just made and think about how we 're going to do it:1) Layout file writing2) mainactivity: Use startactivityforresult() to open registactivity, and override the Onactivityresult () method 3) Registactivity: Use Intent to save the returned data and call setresult(int resultcode, Intent data) to return the resultwith these three steps, you can complete the requirements. So, in addition to this one method, can we have other ways to achieve it? The answer is: offcourse, flexible use of Intent on the linecode implementation: The layout file does not change
    • Mainactivity.java:
public class Mainactivity extends Activity {private EditText et_uname;private EditText et_upass; @Overrideprotected void O Ncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initviews ();} private void Initviews () {et_uname = (EditText) Findviewbyid (r.id.et_uname); et_upass = (EditText) Findviewbyid (r.id.et_ UPass); Intent data = Getintent ();//Get intentif (data! = null) {String uname = Data.getstringextra ("uname"); String UPass = Data.getstringextra ("UPass"); Et_uname.settext (uname); Et_upass.settext (UPass);}} /** * Login * @param view */public void Login (view view) {String uname = Et_uname.gettext (). toString (). Trim (); String UPass = Et_upass.gettext (). toString (). Trim (); if (Textutils.isempty (uname) | | Textutils.isempty (UPass)) {Toast.maketext (this, "username password cannot be empty! ", Toast.length_short). Show ();} else {Toast.maketext (this, "Login successful! ", Toast.length_short). Show ();}} /** * Registration * @param view */public void regist (view view) {Intent Intent = new Intent (This, RegisTactivity.class); startactivity (intent); Finish ();}} 
    • Registactivity.java:
public class Registactivity extends Activity{private EditText et_uname;private EditText Et_upass; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_regist); Initviews ();} private void Initviews () {et_uname = (EditText) Findviewbyid (r.id.et_uname); et_upass = (EditText) Findviewbyid (r.id.et_ UPass);} public void regist (view view) {String uname = Et_uname.gettext (). toString (). Trim (); String UPass = Et_upass.gettext (). toString (). Trim (); if (Textutils.isempty (uname) | | Textutils.isempty (UPass)) {Toast.maketext (this, "username password cannot be empty! ", Toast.length_short). Show ();} else {Toast.maketext (this, "registered successfully! (Toast.length_short). Show ();//Use Intent to save Intent data = new Intent (this, mainactivity.class);d Ata.putextra ("uname" , uname);d Ata.putextra ("UPass", UPass); startactivity (data); Finish ();}} 
After the code is edited, we run the test and find that the results are identical to the above. Well, then this implementation has passed, demand is complete, congratulation!

Copyright Notice: Blog Writing is not easy, reprint, please indicate the source, Thank you very much!

Android Learning Series (v) data transfer between activity

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.