Android--activity receives data returned by another activity

Source: Internet
Author: User

When we jump to another B (activity) in a (activity), and then return a by B, a how to accept the returned data.

We add an input box and a button to the B page, when the input data, click the button to return information to a page, first look at the B page activity code

 PackageCom.example.testapp;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView; Public  class bmainactivity extends Activity {    PrivateTextView T;PrivateEditText T1;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_bmain);        t = (TextView) Findviewbyid (r.id.tv); Intent i = getintent ();//user user = (user) I.getserializableextra ("user");User user = (user) I.getparcelableextra ("User"); T.settext ("Name="+ user.getname () +", age="+ user.getage ());        T1 = (EditText) Findviewbyid (R.ID.EDITTEXT1); Findviewbyid (R.id.button1). Setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {Intent i =NewIntent (); I.putextra ("Data", T1.gettext (). toString ()); Setresult (1, i);            Finish ();    }        }); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.bmain, menu);return true; }}

Add an event to the button in your code and use the Setresult method to return the intent object, and then call the Finish method

Click on the button to return to the a page (this button is how to locate the previous page is still in the research ...) )
The activity code for a is as follows

 PackageCom.example.testapp;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.EditText; Public  class amainactivity extends Activity {    PrivateEditText EditText;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_amain);        EditText = (editText) Findviewbyid (R.ID.EDITTEXT1); Findviewbyid (R.id.button1). Setonclicklistener (NewView.onclicklistener () {@Override                     Public void OnClick(View v) {Intent i =NewIntent (amainactivity. This, Bmainactivity.class); I.putextra ("User",NewUser ("Lijingwei", -));//StartActivity (i);Startactivityforresult (I,0);    }                }); }protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {Super. Onactivityresult (Requestcode, ResultCode, data); Edittext.settext (Data.getstringextra ("Data")); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.amain, menu);return true; }}

Note that we changed startactivity (i) to Startactivityforresult (i,0) and rewritten protected void Onactivityresult (int requestcode, int ResultCode, Intent data) method. Used to receive data returned by B.

Android--activity receives data returned by another 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.