Activity details 3 Start activity and return results. activity details

Source: Internet
Author: User

Activity details 3 Start activity and return results. activity details
First, let's take a look at the demo:

1 Overview

To get the data returned after the Activity is closed, use the startActivityForResult (Intent intent, int requestCode) method provided by the system to open the new Activity, after the new Activity is disabled, data is returned to the previous Activity. To obtain the data returned, the onActivityResult (int requestCode, int resultCode, Intent data) method must be rewritten in the previous Activity.

Public class MainActivity extends Activity {private final static String TAG = "MainActivity"; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Button btnOpen = (Button) this. findViewById (R. id. btnOpen); btnOpen. setOnClickListener (new View. onClickListener () {public void onClick (View v) {// get the data returned after the Activity is closed. // The second parameter is the request code, you can enter startActivityForResult (new Intent (MainActivity. this, OtherActivity. class), 1) ;}}) ;}/ *** to obtain the returned data, the onActivityResult method must be rewritten in the previous Activity (MainActivity class) ** requestCode request code, that is, call startActivityForResult () to pass the previous value * resultCode result code. The result code is used to identify the new Activity from which the returned data comes */@ Override protected void onActivityResult (int requestCode, int resultCode, intent data) {String result = data. getExtras (). getString ("result"); // get the data Log returned after the new Activity is disabled. I (TAG, result );}}

  

When the new Activity is disabled, the data returned by the new Activity is transmitted through Intent. The android platform calls the onActivityResult () method of the previous Activity, input the Intent that stores the returned data as the third input parameter, and use the third input parameter in the onActivityResult () method to retrieve the data returned by the new Activity.

2 setResult

Use the startActivityForResult (Intent intent, int requestCode) method to open a new Activity. Before closing a new Activity, you must use the setResult (int resultCode, Intent data) provided by the system to return data to the previous Activity) method implementation:

Public class OtherActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. other); Button btnClose = (Button) findViewById (R. id. btnClose); btnClose. setOnClickListener (new View. onClickListener () {public void onClick (View v) {// The data is returned using Intent intent = new Intent (); // Save the returned data to Intent intent. putExtra ("result", "My name is linjiqin"); // sets the returned data to OtherActivity. this. setResult (RESULT_ OK, intent); // close Activity OtherActivity. this. the first parameter value of the finish () ;}}}} setResult () method can be defined according to business needs, the RESULT_ OK used in the code above is a constant defined by the system Activity class and its value is-1. The code snippet is as follows: public class android. app. activity extends ...... {public static final int RESULT_CANCELED = 0; public static final int RESULT_ OK =-1; public static final int RESULT_FIRST_USER = 1 ;}

 

Note: When you click "open new Activity", the "I am a new Activity" page is displayed;

Click "close" to close the current page. At the same time, the "I am an old Activity" page is displayed, and the result parameter is passed to the previous Activity.

3. Role of the Request Code

Use the startActivityForResult (Intent intent, int requestCode) method to open a new Activity. We need to input a Request Code (the second parameter) for the startActivityForResult () method ). The value of the Request Code is set by the user according to the business needs, used to identify the request source. For example, a single Activity has two buttons. Clicking these two buttons will open the same Activity. No matter which button opens the new Activity, when the new Activity is closed, the system will call the onActivityResult (int requestCode, int resultCode, Intent data) method of the previous Activity. In the onActivityResult () method, if you need to know that the new Activity is opened by that button and want to handle the corresponding business, you can do this:

 

@ Override public void onCreate (Bundle savedInstanceState ){.... button1.setOnClickListener (new View. onClickListener () {public void onClick (View v) {startActivityForResult (new Intent (MainActivity. this, NewActivity. class), 1) ;}}); button2.setOnClickListener (new View. onClickListener () {public void onClick (View v) {startActivityForResult (new Intent (MainActivity. this, NewActivity. class), 2) ;}}); @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {switch (requestCode) {case 1: // request from button 1 for corresponding business processing case 2: // request from button 2 for corresponding business processing }}}

  

4. Result code

In an Activity, you may use the startActivityForResult () method to open multiple different activities to process different businesses. When these new activities are closed, the system will call the onActivityResult (int requestCode, int resultCode, Intent data) method of the previous Activity. To know which new Activity the returned data is from, you can do this in the onActivityResult () method (ResultActivity and NewActivity are the new Activity to be opened ):

Public class ResultActivity extends Activity {..... resultActivity. this. setResult (1, intent); ResultActivity. this. finish ();} public class NewActivity extends Activity {...... newActivity. this. setResult (2, intent); NewActivity. this. finish ();} public class MainActivity extends Activity {// in this Activity, the ResultActivity and NewActivity @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) will be opened) {switch (resultCode) {case 1: // ResultActivity returned data case 2: // NewActivity returned data }}}
5 Demo source code:

TestResultActivity. java

Package mm.shandong.com. testresult; import android. content. intent; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. view. view; import android. widget. editText; import android. widget. radioButton; import android. widget. radioGroup; import android. widget. textView; public class TestResultActivity extends AppCompatActivity {EditText editTextBrand; RadioGroup radioGroup; TextView textViewXH; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_test_result); editTextBrand = (EditText) findViewById (R. id. editTextBrand); radioGroup = (RadioGroup) findViewById (R. id. radioGroup); textViewXH = (TextView) findViewById (R. id. textViewXH); RadioButton radionButton = (RadioButton) radioGroup. getChildAt (0); radionButton. setChecked (true);} // jump to the selection brand page public void selectBrand (View view) {Intent intent = new Intent (this, TestResultActivity1.class); startActivityForResult (intent, 1 );} /// select the public void selectCompute (View view) {Intent intent = new Intent (this, TestResultActivity2.class); String brand = editTextBrand. getText (). toString (); RadioButton radionButton = (RadioButton) radioGroup. findViewById (radioGroup. getCheckedRadioButtonId (); String nc = radionButton. getText (). toString (); intent. putExtra ("brand", brand); intent. putExtra ("nc", nc); startActivityForResult (intent, 2); textViewXH. setText ("");} // callback @ Override protected void onActivityResult (int requestCode, int resultCode, Intent intent) returned by the activity request {switch (requestCode) {// resultCode indicates the flag of the callback: case 1: if (resultCode = 2) {String brand = intent. getStringExtra ("brand"); editTextBrand. setText (brand);} break; case 2: if (resultCode = 3) {String xh = intent. getStringExtra ("xh"); textViewXH. setText (xh) ;}break ;}}}

TestResultActivity1

Package mm.shandong.com. testresult; import android. content. intent; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. widget. arrayAdapter; import android. widget. listView; import android. widget. toast; public class TestResultActivity1 extends AppCompatActivity {ListView listView; String [] brands = new String [] {"Lenovo", "Dell"}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_test_result1); listView = (ListView) findViewById (R. id. listView); ArrayAdapter arrayAdapter = new ArrayAdapter (this, android. r. layout. simple_list_item_checked, brands); listView. setAdapter (arrayAdapter); listView. setChoiceMode (ListView. CHOICE_MODE_SINGLE);} // submit the Selected Brand result public void submit (View view) {int index = listView. getCheckedItemPosition (); if (index <0) {Toast. makeText (this, "select brand", Toast. LENGTH_SHORT ). show (); return;} String brand = (String) listView. getItemAtPosition (index); int resultCode = 2; Intent intent = getIntent (); intent. putExtra ("brand", brand); setResult (2, intent); finish ();}}

 

 

TestResultActivity2

Package mm.shandong.com. testresult; import android. content. intent; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. widget. arrayAdapter; import android. widget. listView; import android. widget. toast; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; public class TestResultActivity2 extends AppCompatActivity {ListView listView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_test_result2); Intent intent = getIntent (); String brand = intent. getStringExtra ("brand"); String nc = intent. getStringExtra ("nc"); Map map = initData (); List <String> lists = (List <String>) map. get (brand + nc); listView = (ListView) findViewById (R. id. listView); ArrayAdapter arrayAdapter = new ArrayAdapter (this, android. r. layout. simple_list_item_checked, lists); listView. setAdapter (arrayAdapter); listView. setChoiceMode (ListView. CHOICE_MODE_SINGLE);} // submit the selected computer model public void submit (View view) {int index = listView. getCheckedItemPosition (); if (index <0) {Toast. makeText (this, "select a model", Toast. LENGTH_SHORT ). show (); return;} String xh = (String) listView. getItemAtPosition (index); int resultCode = 2; Intent intent = getIntent (); intent. putExtra ("xh", xh); setResult (3, intent); finish () ;}// initialize the public Map initData () {Map map = new HashMap (); list <String> lists = new ArrayList <> (); lists. add ("Lenovo 1G0001"); lists. add ("Lenovo 1G0002"); map. put ("Lenovo 1G", lists); lists = new ArrayList <> (); lists. add ("Lenovo 2G0001"); lists. add ("Lenovo 2G0002"); map. put ("Lenovo 2G", lists); lists = new ArrayList <> (); lists. add ("Dell 1G0001"); lists. add ("Dell 1G0002"); map. put ("Dell 1G", lists); lists = new ArrayList <> (); lists. add ("Dell 2G0001"); lists. add ("Dell 2G0002"); map. put ("Dell 2G", lists); return map ;}}

 


 

 

  Download Demo
Finally, the above examples all come from the same source as Android. Please download the app treasure or pea pod:Http://android.myapp.com/myapp/detail.htm? ApkName = com. shandong. mm. androidstudySource code example documentation is exhausted

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.