MainActivity:
package com.example.startactivityforresutl;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity activity = this;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(activity,ActivityB.class);
intent.putExtra(Contents.MainActivity_KEY, "hello ! I am from MainActivity!");
startActivityForResult(intent,Contents.request_code);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(this.getClass().getName()+":onActivityResult","requestCode:"+requestCode+" resultCode:"+resultCode);
Log.d(this.getClass().getName()+" :onActivityResult", data.getStringExtra(Contents.ActivityB_KEY));
}
}
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.startactivityforresutl.MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="60dp"
android:layout_marginTop="90dp"
android:text="启动ActivityB" />
</RelativeLayout>
ActivityB:
package com.example.startactivityforresutl;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class ActivityB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Intent intent = this.getIntent();
String s = intent.getStringExtra(Contents.MainActivity_KEY);
Log.d("ActivityB输出", s);
final Activity activity = this;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity,MainActivity.class);
intent.putExtra(Contents.ActivityB_KEY,"hello ! I am form Activtiy B !" );
activity.setResult(Contents.result_code,intent);
activity.finish();
}
});
}
}
activity_b.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.startactvityforresutl.MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="60dp"
android:layout_marginTop="90dp"
android:text="返回MainActivity" />
</RelativeLayout>
Contents:
package com.example.startactivityforresutl;
public class Contents {
public static final String MainActivity_KEY="key";
public static final String ActivityB_KEY="key";
public static final int request_code=9;
public static final int result_code=10;
}
<activity
android:name=".ActivityB"
android:label="B" >
</activity>