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