Four components of Android-activation of Activity: StartActivity () and StartActivityForResult () (graphic explanation)

Source: Internet
Author: User

Four components of Android-activation of Activity: StartActivity () and StartActivityForResult () (graphic explanation)
[Four major Android component Learning Series Activity] 1. four components of Android-Activity (1) definition, status, and rollback (graphic explanation) 2. four components of Android-Activity lifecycle (graphic explanation) 3. the four main components of Android-the activation of StartActivity () and StartActivityForResult () (graphic explanation) of the Activity learned the basic concepts and lifecycle of the Activity for the first two times, today, let's take a look at Activity jump and value transfer. Start learning! There are two methods to enable a new Activity: startActivity (Intent) and startActivityForResult (Intent, int ). StartActivity (Intent): Used to start a new Activity, which is pushed to the top of the stack. The Intent parameter is used to describe the new Activity. StartActivityForResult (Intent, int): Used to start a new Activity. After the new Activity exits, the result is returned to the old Activity. StartActivity is easy to understand, and startActivityForResult () is a little troublesome. The following figure makes it easy for us to understand: Description: requestCode, request code, self-configured, but must be non-negative. This code is returned to onActivityResult () only when the Activity exits when it is not a negative number (). ResultCode, result code, used to identify the new Activity from which the returned data comes from. Its value is generally RESULT_CANCELED, RESULT_ OK, RESULT_FIRST_USER. I wrote a Demo about how to enable the Activity: a total of three activities: MainActivity Users enter the user name, phone number, city, CityActivity for the user to select the city, ShowActivity is used to display the user information. MainActivity: Copy code 1 package com. johntsai. startactivity; 2 3 import android. app. activity; 4 import android. content. intent; 5 import android. database. cursor; 6 import android.net. uri; 7 import android. OS. bundle; 8 import android. provider. contactsContract. commonDataKinds. phone; 9 import android. view. view; 10 import android. view. view. onClickListener; 11 import android. widget. button; 12 import android. widget. editText; 13 14 public class MainActivity extends Activity implements OnClickListener {15 16 private Button button_city, button_phone, button_save; 17 private EditText editText_city, editText_name, editText_phone; 18 private static final int Limit = 2; 19 private static final int SELECT_PHONE_REQUEST = 3; 20 21 @ Override22 protected void onCreate (Bundle savedInstanceState) {23 super. onCreate (savedInstanceState); 24 setContentView (R. layout. activity_main); 25 26 button_city = (Button) findViewById (R. id. button_city); 27 button_phone = (Button) findViewById (R. id. button_phone); 28 button_save = (Button) findViewById (R. id. button_save); 29 editText_city = (EditText) findViewById (R. id. editText_city); 30 editText_name = (EditText) findViewById (R. id. editText_name); 31 editText_phone = (EditText) findViewById (R. id. editText_Phone); 32 33 listener (this); 34 button_phone.setOnClickListener (this); 35 button_save.setOnClickListener (this); 36} 37 @ Override38 public void onClick (View v) {39 switch (v. getId () {40 case R. id. button_city: 41 Intent I = new Intent (); 42 I. setClass (MainActivity. this, CityActivity. class); 43 startActivityForResult (I, SELECT_CITY_REQUEST); 44 break; 45 case R. id. button_phone: 46 Intent pickContactIntent = new Intent (Intent. ACTION_PICK, Uri. parse ("content: // contacts"); 47 pickContactIntent. setType (Phone. CONTENT_TYPE); 48 startActivityForResult (pickContactIntent, SELECT_PHONE_REQUEST); 49 break; 50 case R. id. button_save: 51 String name = editText_name.getText (). toString (); 52 String city = editText_city.getText (). toString (); 53 String phone = editText_phone.getText (). toString (); 54 Intent saveIntent = new Intent (MainActivity. this, ShowActivity. class); 55 saveIntent. putExtra ("content", "NAME:" + name + "\ nCITY:" + city + "\ nPHONE" + phone); 56 startActivity (saveIntent); 57 break; 58 default: 59 break; 60} 61} 62 63 @ Override64 protected void onActivityResult (int requestCode, int resultCode, Intent data) {65 if (SELECT_CITY_REQUEST = requestCode) {66 if (resultCode = RESULT_ OK) {67 String city = data. getStringExtra ("city"); 68 editText_city.setText (city); 69} 70} 71 if (SELECT_PHONE_REQUEST = requestCode) {72 if (resultCode = RESULT_ OK) {73 Uri contactData = data. getData (); 74 String [] projection = {Phone. NUMBER}; 75 Cursor c = getContentResolver () 76. query (contactData, projection, null); 77 c. moveToFirst (); 78 int column = c. getColumnIndex (Phone. NUMBER); 79 String num = c. getString (column); 80 editText_phone.setText (num); 81} 82} 83 super. onActivityResult (requestCode, resultCode, data); 84} 85} 1 package com. johntsai. startactivity; 2 3 4 import android. app. activity; 5 import android. content. intent; 6 import android. OS. bundle; 7 import android. view. gravity; 8 import android. view. view; 9 import android. view. viewGroup; 10 import android. widget. absListView; 11 import android. widget. baseExpandableListAdapter; 12 import android. widget. expandableListAdapter; 13 import android. widget. expandableListView; 14 import android. widget. linearLayout; 15 import android. widget. textView; 16 import android. widget. toast; 17 18 public class CityActivity extends Activity {19 20 private ExpandableListView eListView; 21 @ Override 22 protected void onCreate (Bundle savedInstanceState) {23 24 super. onCreate (savedInstanceState); 25 setContentView (R. layout. layout_city); 26 initEListView (); 27} 28 29 // initialize 30 private void initEListView () {31 ExpandableListAdapter adapter = new BaseExpandableListAdapter () {32 33 34 private String [] countries = new String [] {35 "China", "USA", "UK" 36 }; 37 38 private String [] [] cities = new String [] [] {39 {"BeiJing", "ShangHai", "HongKong", "TaiBei "}, 40 {"Washington", "New York", "Los Angles", "San Fransico"}, 41 {"London", "Manchester", "birmheim ", "Liverpool"} 42}; 43 private TextView getTextView () {44 AbsListView. layoutParams lp = new AbsListView. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, 45 ViewGroup. layoutParams. WRAP_CONTENT); 46 TextView textView = new TextView (CityActivity. this); 47 textView. setLayoutParams (lp); 48 textView. setGravity (Gravity. CENTER_HORIZONTAL); 49 textView. setPadding (0, 0, 0, 0); 50 textView. setTextSize (20); 51 return textView; 52 53} 54 @ Override 55 public boolean isChildSelectable (int groupPosition, int childPosition) {56 Toast. makeText (CityActivity. this, "you have clicked Group:" + groupPosition + "Child:" + childPosition + "\ n" + countries [groupPosition] + "" + cities [groupPosition] [childPosition], 57 Toast. LENGTH_SHORT ). show (); 58 Intent I = new Intent (); 59 I. putExtra ("city", cities [groupPosition] [childPosition]); 60 setResult (RESULT_ OK, I); 61 finish (); 62 return true; 63} 64 65 @ Override 66 public boolean hasStableIds () {67 return true; 68} 69 70 @ Override 71 public View getGroupView (int groupPosition, boolean isExpanded, 72 View convertView, viewGroup parent) {73 LinearLayout linearLayout = new LinearLayout (CityActivity. this); 74 linearLayout. setOrientation (0); 75 76 TextView textView = getTextView (); 77 textView. setText (getGroup (groupPosition ). toString (); 78 linearLayout. addView (textView); 79 return linearLayout; 80} 81 @ Override 82 public long getGroupId (int groupPosition) {83 return groupPosition; 84} 85 86 @ Override 87 public int getGroupCount () {88 return countries. length; 89} 90 91 @ Override 92 public Object getGroup (int groupPosition) {93 return countries [groupPosition]; 94} 95 96 @ Override 97 public int getChildrenCount (int groupPosition) {98 return cities [groupPosition]. length; 99} 100 101 @ Override102 public View getChildView (int groupPosition, int childPosition, 103 boolean isLastChild, View convertView, ViewGroup parent) {104 TextView textView = getTextView (); 105 textView. setText (getChild (groupPosition, childPosition ). toString (); 106 textView. setGravity (Gravity. CENTER_HORIZONTAL); 107 return textView; 108} 109 110 @ Override111 public long getChildId (int groupPosition, int childPosition) {112 return childPosition; 113} 114 115 @ Override116 public Object getChild (int groupPosition, int childPosition) {117 return cities [groupPosition] [childPosition]; 118} 119 eListView = (ExpandableListView) findViewById (R. id. expandableListView); 121 eListView. setAdapter (adapter); 122} 123} copy code 1 package com. johntsai. startactivity; 2 3 import android. app. activity; 4 import android. content. intent; 5 import android. OS. bundle; 6 import android. widget. textView; 7 8 public class ShowActivity extends Activity {9 private TextView showTextView; 10 @ Override11 protected void onCreate (Bundle savedInstanceState) {12 super. onCreate (savedInstanceState); 13 setContentView (R. layout. layout_show); 14 showTextView = (TextView) findViewById (R. id. textView_show); 15 16 Intent I = getIntent (); 17 String content = I. getStringExtra ("content"); 18 showTextView. setText (content); 19} 20}

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.