代码如下:
package com.zzw.teststartintentforrequest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private final int REQUESTCODE_MAIN = 1;
private final String KEY_MAIN = "key_main";
private final String KEY_B = "key_b";
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView_main);
findViewById(R.id.button_main).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActivityB.class);
int a=(int) (Math.random()*100);
int b=(int) (Math.random()*100);
int[] nums = { a, b };
intent.putExtra(KEY_MAIN, nums);
startActivityForResult(intent, REQUESTCODE_MAIN);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data!=null){
Log.d("-------", "requestCode:"+requestCode+" resultCode:"+resultCode);
textView.setText(data.getIntExtra(KEY_B, 0)+"");
}
}
}
MainActivity.java
package com.zzw.teststartintentforrequest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class ActivityB extends Activity {
private final int RESULTCODE_B=2;
private final String KEY_MAIN="key_main";
private final String KEY_B="key_b";
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityb);
textView=(TextView) findViewById(R.id.textView_B);
int nums[]=this.getIntent().getIntArrayExtra(KEY_MAIN);
final int sum=nums[0]+nums[1];
textView.setText(nums[0]+"+"+nums[1]+"="+sum);
findViewById(R.id.button_B).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(ActivityB.this,MainActivity.class);
intent.putExtra(KEY_B,sum);
ActivityB.this.setResult(RESULTCODE_B, intent);
ActivityB.this.finish();
}
});
}
}
ActivityB.java
<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:background="@android:color/holo_blue_light"
tools:context="com.zzw.teststartintentforrequest.MainActivity" >
<TextView
android:id="@+id/textView_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="0"
android:textColor="@android:color/holo_red_light"
android:textSize="30sp" />
<Button
android:id="@+id/button_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:background="@android:color/holo_blue_light"
android:text="点击跳转AcyivityB"
android:textColor="@android:color/holo_red_light" />
</RelativeLayout>
activity_main.xml
<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:background="@android:color/black"
tools:context="com.zzw.teststartintentforrequest.MainActivity" >
<TextView
android:id="@+id/textView_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="30sp" />
<Button
android:id="@+id/button_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:background="@android:color/black"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:text="点击返回MainAcyivity" />
</RelativeLayout>
activityb.xml