Implementation Method for processing returned results by Activity in Android, androidactivity

Source: Internet
Author: User

Implementation Method for processing returned results by Activity in Android, androidactivity

Everyone has this experience during online shopping. When you confirm the selection of the recipient and address for the order, the page will jump to all the receiving information (including the Receiving address and the recipient) stored on our website) once we click a piece of information, it will automatically jump to the order submission page. At this time, the receipt information has changed to the previously selected receiving information,

To implement this function, Android provides a mechanism to return a jump to another activity and receive the value returned by other activities without the need to start a new activity. In the following example, create two activities. In MainActivity, the system prompts you to enter "your domain name". Then, click TextView "your domain name" to start Main2Activity, select a domain name in Main2Activity, And then when MainActivity is returned, returns the original data selected in Main2Activity to MainActivity. The implementation result is as follows:

The core implementation steps are as follows:

(1) set the start of the Activity with the result requestCode in MainActivity.

(2) set the method for processing the returned results in MainActivity.

(3) set the returned results in Main2Activity.

The specific layout and function implementation Code are as follows: (1) layout Activity_main.xml homepage layout:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout 3 xmlns: android = "http://schemas.android.com/apk/res/android" 4 xmlns: tools = "http://schemas.android.com/tools" 5 android: id = "@ + id/activity_main" 6 android: layout_width = "match_parent" 7 android: layout_height = "match_parent" 8 android: orientation = "vertical" 9 tools: context = "com. example. administrator. testactivity. mainActivity "> 10 <TextView11 android: id =" @ + id/tv1 "12 android: layout_width =" match_parent "13 android: layout_height =" wrap_content "14 android: hint = "pan Houye's Sn" 15 android: gravity = "center" 16 android: textSize = "30sp"/> 17 <Button18 android: id = "@ + id/btn1" 19 android: layout_width = "wrap_content" 20 android: layout_height = "wrap_content" 21 android: layout_gravity = "right" 22 android: text = "OK"/> 23 </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" 3     xmlns:tools="http://schemas.android.com/tools" 4     android:id="@+id/activity_main2" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     tools:context="com.example.administrator.day15.Main2Activity"> 8     <ListView 9         android:id="@+id/lv"10         android:layout_width="match_parent"11         android:layout_height="match_parent"></ListView>12 </RelativeLayout>
(3) MainActivity. java implementation code of the homepage layout in java:
1 import android. content. intent; 2 import android. support. v7.app. appCompatActivity; 3 import android. OS. bundle; 4 import android. view. gravity; 5 import android. view. view; 6 import android. widget. button; 7 import android. widget. textView; 8 import android. widget. toast; 9 public class MainActivity extends AppCompatActivity {10 private Button btn1; 11 private TextView tv1; 12 @ Override13 protected void onCreate (Bundle savedInstanceState) {14 super. onCreate (savedInstanceState); 15 setContentView (R. layout. activity_main); 16 btn1 = (Button) findViewById (R. id. btn1); 17 tv1 = (TextView) findViewById (R. id. tv1); 18 tv1.setClickable (true); 19 tv1.setOnClickListener (new View. onClickListener () {20 @ Override21 public void onClick (View v) {22 Intent intent = new Intent (MainActivity. this, Main2Activity. class); 23 // set the startup intent and the corresponding request code 24 startActivityForResult (intent, 2); 25} 26}); 27 btn1.setOnClickListener (new View. onClickListener () {28 @ Override29 public void onClick (View v) {30 Toast t = Toast. makeText (MainActivity. this, tv1.getText (). toString (), Toast. LENGTH_SHORT); 31 t. setGravity (Gravity. TOP, 0, 0); 32 t. show (); 33} 34}); 35} 36 @ Override37 protected void onActivityResult (int requestCode, int resultCode, Intent data) {38 super. onActivityResult (requestCode, resultCode, data); 39 // we recommend that you add a logical judgment (pairing the Request Code with the result code to ensure that the returned result is requested by the Request Code) 40 tv1.setText (data. getStringExtra ("select"); 41} 42}
(4) In java, the second page layout is Main2Activity. java implementation code:
1 import android. content. intent; 2 import android. support. v7.app. appCompatActivity; 3 import android. OS. bundle; 4 import android. view. view; 5 import android. widget. adapterView; 6 import android. widget. arrayAdapter; 7 import android. widget. listView; 8 public class Main2Activity extends AppCompatActivity {9 private ListView lv; 10 String [] names = {"horse", "Ox", "Tiger", "rabbit ", "Dragon"}; 11 @ Override12 protected void onC Reate (Bundle savedInstanceState) {13 super. onCreate (savedInstanceState); 14 setContentView (R. layout. activity_main2); 15 lv = (ListView) findViewById (R. id. lv); 16 // use the ArrayAdapter to fill in 17 ArrayAdapter data for each ListView cell <String> adapter = new ArrayAdapter <String> (Main2Activity. 18 this, android. r. layout. simple_list_item_single_choice, names); 19 // set ListView to single-choice mode 20 lv. setChoiceMode (ListView. CHOICE_MODE_SINGLE); 21 // Click Event 22 lv. setOnItemClickListener (new AdapterView. OnItemClickListener () {23 @ Override24 public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {25 Intent intent = new Intent (Main2Activity. this, MainActivity. class); 26 // mount the information corresponding to the selected item with the key value to Extra for passing 27 intent. putExtra ("select", names [position]); 28 // set 3 to result code 29 setResult (3, intent); 30 finish (); // ends the current Activity, returns the previous activity31} 32}); 33 lv. setAdapter (adapter); 34} 35}

 

Related Article

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.