Nonsense not to say more, directly to everyone paste code.
Package com.example.testactivityresquest;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {sup
Er.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Button btn = (button) Findviewbyid (R.id.button); Btn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Intent int
ent = new Intent (mainactivity.this, Activityb.class);
Int[] Nums = {,};
Intent.putextra (Changliang.key, nums); Unlike StartActivity, if the other activity starts more later.
The equivalent of a specified key value, returned according to the key value.
Startactivityforresult (Intent, Changliang.requestcode);
}
}); //ACTIVITYB data returned in this method gets @Override protected void onActivityresult (int requestcode, int resultcode, Intent data) {int s = Data.getintextra (Changliang.activity_b_key,);
Toast.maketext (Getapplicationcontext (), "passing two numbers get and is:" + S,). Show ();
}} package com.example.testactivityresquest;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast; public class Activityb extends activity {@Override protected void onCreate (Bundle savedinstancestate) {super.
OnCreate (savedinstancestate);
Setcontentview (R.LAYOUT.ACTIVITYB);
Intent Intent = This.getintent ();
int[] n = Intent.getintarrayextra (Changliang.key);
Final int nums = n[] + n[];
Toast.maketext (this, n[] + "" + n[],). Show ();
Button btn = (button) Findviewbyid (R.id.button); Btn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (VieW v) {Intent Intent = new Intent (activityb.this, Mainactivity.class);
Intent.putextra (Changliang.activity_b_key, nums);
Import data into Activityb.this.setResult (Changliang.requestcode, intent) based on the intent event of a particular key value;
Return to main activity Activityb.this.finish () after shutdown;
}
});
}} package com.example.testactivityresquest; public class Changliang {public static final string key= ' KEY '; public static final string activity_b_key= ' Key1 '; public s
tatic final int requestcode=1987; }
XML file :
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "android:backg" round= "#ff" > <button android:id= "@+id/button" android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true" android:l Ayout_margintop= "DP" android:text= "Start Activityb"/> </RelativeLayout> <?xml version= "." Encoding= "utf-"? > <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent "android:layout_height=" match_parent "android:background=" #ff "android:orientation=" vertical "> <
Button android:id= "@+id/button" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text= "Return to activity"/> </linearlayouT>
Don't forget to register Activityb in Androidmanifast.
Run Effect chart:
The difference between Startactivityforresult and startactivity is:
1, StartActivity ()
Just jump to the target page, if you want to jump back to the current page, you must use the StartActivity () again.
2, Startactivityforresult ()
You can complete this task at once, and when the program executes to this code, if you jump from t1activity to the next text2activity, and when the Text2activity calls the finish () method, the program automatically jumps back t1activity , and calls the Onactivityresult () method in the previous t1activity.