Example of Android returning the results to the main interface from the main activity value to the child activity

Source: Internet
Author: User

In the original document is: Start Activity and Getting Results

The StartActivity (android.content.Intent) method is used to start aNewactivity, which'll is placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed. Sometimes you want to get a result back from the activity when it ends. For example, you could start an activity that lets the user pick a WHO in a list of contacts; When it ends, it returns the person is selected. to Do  This, you call the Startactivityforresult (Intent,int) with Startactivityforresult (Intent,int) method instead of the original StartActivity method. Version with a second integer parameter identifying the call. The result would come back through your Onactivityresult (int,int, Android.content.Intent) method. When a activity exits, it can call Setresult (int) toreturnData back to its parent. It must always supply a result code, which can is the standard results result_canceled, RESULT_OK, or any custom values St Arting at Result_first_user. In addition, it can optionallyreturnBack a Intent containing any additional data it wants. All of ThisInformation appears back on the parent ' s Activity.onactivityresult (),rewrite the Onactivity method in the main activity to get the results returned! Along with the integer identifier it originally supplied. If A child activity fails forAny reason (such as crashing), the parent activity would receive a result with the code result_canceled. Public classMyActivityextendsActivity {...Static Final intPick_contact_request = 0; protected BooleanOnKeyDown (intKeyCode, KeyEvent event) {         if(KeyCode = =keyevent.keycode_dpad_center) {             //When the user Center is presses, let them pick a contact.Startactivityforresult (NewIntent (Intent.action_pick,NewUri ("Content://contacts") ), pick_contact_request); return true; }         return false; }     protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {         if(Requestcode = =pick_contact_request) {             if(ResultCode = =RESULT_OK) {                 //A contact is picked. Here we'll just display it//To the user.StartActivity (NewIntent (Intent.action_view, data)); }         }     } }

In the main activity

The focus is to replace the original StartActivity method with the Startactivityforresult (Intent, requestcode) method.

and added: Onactivityresult (int requestcode, int resultcode, Intent data) method to get the results returned by the child activity

Your own program:

1  PackageCom.example.saagr;2 3 ImportAndroid.os.Bundle;4 Importandroid.app.Activity;5 Importandroid.content.Intent;6 ImportAndroid.view.Menu;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 ImportAndroid.widget.Button;Ten ImportAndroid.widget.EditText; One ImportAndroid.widget.TextView; A  -  Public classMainactivityextendsActivity { -     PrivateEditText EditText, EditText2, editText3; the     Privatebutton button; -     PrivateTextView TextView, textView2; -  - @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); Abutton =(Button) Findviewbyid (r.id.but_post); atEditText =(EditText) Findviewbyid (R.ID.ET1); -EDITTEXT2 =(EditText) Findviewbyid (R.id.et2); -EDITTEXT3 =(EditText) Findviewbyid (R.ID.ET3); -TextView =(TextView) Findviewbyid (r.id.tv_add); -TextView2 =(TextView) Findviewbyid (r.id.tv_equ); -Button.setonclicklistener (NewOnclicklistener () { in  - @Override to              Public voidOnClick (View v) { +                 //TODO auto-generated Method Stub -String S1 =Edittext.gettext (). toString (); theString s2 =Edittext2.gettext (). toString (); *Intent Intent =NewIntent (mainactivity. This, subactivity.class); $Intent.putextra ("message", S1 + "+" + s2 + "=" + "?");Panax NotoginsengStartactivityforresult (Intent, 1000); -  the             } +         }); A  the     } +  - @Override $     protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { $         //TODO auto-generated Method Stub -         Super. Onactivityresult (Requestcode, ResultCode, data); -         if(Requestcode = = && ResultCode = = 1001) { theString result = Data.getstringextra ("Result"); - Edittext3.settext (result);Wuyi         } the     } -  Wu @Override -      Public BooleanOncreateoptionsmenu (Menu menu) { About         //inflate the menu; This adds items to the action bar if it is present. $ getmenuinflater (). Inflate (R.menu.main, menu); -         return true; -     } -  A}
View Code

In the child activity

Returns the result to the main activity using the Setresult (int resultcode, Intent data) method

Your own program:

 PackageCom.example.saagr;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView; Public classSubactivityextendsActivity {PrivateTextView TextView; PrivateEditText EditText; Privatebutton button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_sub); TextView=(TextView) Findviewbyid (r.id.tv_show); EditText=(EditText) Findviewbyid (r.id.et); Button=(Button) Findviewbyid (r.id.but); Intent Intent=getintent (); String message= Intent.getstringextra ("message");        Textview.settext (message); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubString result =Edittext.gettext (). toString (); Intent Intent=NewIntent (); Intent.putextra ("Result", result); Setresult (1001, intent);            Finish ();    }        }); }}
View Code

Summarize:

Main activity with startactivityforresult (Intent,

Child activity uses getintent () to get the data sent over and To process, and then encapsulate the resulting data into a intent with Setresult (int Resultcode, Intent Intent ) return to the main activity (ResultCode is also used to identify);

Main activity re-use onactivityresult (int requestcode, int resultcode, Intent data) The method gets the result returned and the judgment statement is defined in this method body to determine if the Requestcode and ResultCode match the corresponding identity, and then the result is processed

Example of Android returning the results to the main interface from the main activity value to the child 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.