How activity processing returns results in Android

Source: Internet
Author: User

Everyone in the online shopping has such an experience, in the confirmation of the order to select the consignee and address, will jump to the page to us to deposit all receipt information (including delivery address, consignee) interface for us to choose, once we click on one of the information, it will automatically jump to the order submission interface, The receipt information at this time has changed to our previously selected pickup information,

To achieve this, Android provides a mechanism to jump to other activity, return it, and accept the value returned by other activity without having to start the new active activity; In the following example, create two activity, Where you are prompted for "your Zodiac" in Mainactivity, then click TextView "Your Zodiac" to start main2activity, select Main2activity in Zodiac, and then when you return to Mainactivity, Returns the data selected in the previous main2activity to Mainactivity. Implementation results such as:

The core implementation steps are as follows:

(1) Set up activity in mainactivity with result Requestcode

(2) Set the method to process the returned result in mainactivity

(3) Set the return result in main2activity

The specific layout and function implementation code are as follows: (1) layout activity_main.xml Home layout:
1<?xml version= "1.0" encoding= "Utf-8"?>2<LinearLayout3Xmlns:android= "Http://schemas.android.com/apk/res/android"4Xmlns:tools= "Http://schemas.android.com/tools"5Android:id= "@+id/activity_main"6Android:layout_width= "Match_parent"7android:layout_height= "Match_parent"8android:orientation= "Vertical"9tools:context= "Com.example.administrator.testactivity.MainActivity" >Ten<TextView OneAndroid:id= "@+id/tv1" AAndroid:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -android:hint= "Pan Hou's Zodiac" theandroid:gravity= "Center" -Android:textsize= "30sp"/> -<Button -Android:id= "@+id/btn1" +Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" +Android:layout_gravity= "Right" Aandroid:text= "OK"/> at</LinearLayout>
(2) Layout activity_main2.xml second page layout:
1<?xml version= "1.0" encoding= "Utf-8"?>2<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Xmlns:tools= "Http://schemas.android.com/tools"4Android:id= "@+id/activity_main2"5Android:layout_width= "Match_parent"6android:layout_height= "Match_parent"7tools:context= "Com.example.administrator.day15.Main2Activity" >8<ListView9Android:id= "@+id/lv"TenAndroid:layout_width= "Match_parent" Oneandroid:layout_height= "Match_parent" ></ListView> A</RelativeLayout>
(3) Java in the first page layout Mainactivity.java implementation code:
1 Importandroid.content.Intent;2 Importandroid.support.v7.app.AppCompatActivity;3 ImportAndroid.os.Bundle;4 Importandroid.view.Gravity;5 ImportAndroid.view.View;6 ImportAndroid.widget.Button;7 ImportAndroid.widget.TextView;8 ImportAndroid.widget.Toast;9  Public classMainactivityextendsappcompatactivity {Ten     PrivateButton btn1; One     PrivateTextView TV1; A @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); -BTN1 =(Button) Findviewbyid (R.ID.BTN1); -TV1 =(TextView) Findviewbyid (R.ID.TV1); -Tv1.setclickable (true); +Tv1.setonclicklistener (NewView.onclicklistener () { - @Override +              Public voidOnClick (View v) { AIntent Intent =NewIntent (mainactivity. This, Main2activity.class); at                 //Set the startup intent and corresponding request code -Startactivityforresult (Intent, 2); -             } -         }); -Btn1.setonclicklistener (NewView.onclicklistener () { - @Override in              Public voidOnClick (View v) { -Toast t= Toast.maketext (mainactivity. This, Tv1.gettext (). toString (), toast.length_short); toT.setgravity (gravity.top,0,0); + t.show (); -             } the         }); *     } $ @OverridePanax Notoginseng     protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { -         Super. Onactivityresult (Requestcode, ResultCode, data); the         //It is recommended to add logical judgment (to pair the request code with the result code to ensure that the return result is requested by the request code) +Tv1.settext (Data.getstringextra ("select")); A     } the}
(4) the second page layout in Java Main2activity.java Implementation code:
1 Importandroid.content.Intent;2 Importandroid.support.v7.app.AppCompatActivity;3 ImportAndroid.os.Bundle;4 ImportAndroid.view.View;5 ImportAndroid.widget.AdapterView;6 ImportAndroid.widget.ArrayAdapter;7 ImportAndroid.widget.ListView;8  Public classMain2activityextendsappcompatactivity {9     PrivateListView LV;TenString[] names={"horse", "ox", "Tiger", "Rabbit", "dragon"}; One @Override A     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main2); theLV =(ListView) Findviewbyid (r.id.lv); -         //use Arrayadapter to populate data for each cell in the ListView -arrayadapter<string> adapter =NewArrayadapter<string>(main2activity. -                  This, Android. R.layout.simple_list_item_single_choice,names); +         //set ListView to Radio mode - Lv.setchoicemode (listview.choice_mode_single); +         //set the Click event for each iterm in the ListView ALv.setonitemclicklistener (NewAdapterview.onitemclicklistener () { at @Override -              Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) { -Intent Intent =NewIntent (main2activity. This, Mainactivity.class);; -                 //The information corresponding to the selected item is loaded with the key value in the extra for delivery -Intent.putextra ("select", Names[position]); -                 //Set 3 As the result code inSetresult (3, intent); -Finish ();//end Current activity, return to previous activity to             } +         }); - Lv.setadapter (adapter); the     } *}

How activity processing returns results in Android

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.