一、 代码
1. MainActivity.java
package com.receiveresult;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
publicclass MainActivity extends Activity {
staticfinalprivateintGET_CODE = 0;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new Button.OnClickListener(){
publicvoid onClick(View v){
Intent intent = new Intent();
intent.setClass(MainActivity.this,ReceiverActivity.class);
startActivityForResult(intent, GET_CODE);
}
});
Log.v("MainActivity", "onCreate");
}
/**
* requestCode就是startActivityForResult的requestCode,resultCode就是setResult里面的resultCode,返回的数据在data里面。
*/
@Override
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_CODE) {
TextView text = (TextView) findViewById(R.id.textView1);
if (resultCode == RESULT_CANCELED) {
text.setText("cancelled");
} else {
if (data != null) {
text.setText(data.getAction());
}
}
}
Log.v("MainActivity", "onActivityResult");
}
}
2. ReceiverActivity.java
package com.receiveresult;
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;
publicclass ReceiverActivity extends Activity {
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receiver_main);
Button ok = (Button)findViewById(R.id.ok);
ok.setOnClickListener(okListener);
Button fail = (Button)findViewById(R.id.fail);
fail.setOnClickListener(failListener);
}
private OnClickListener okListener = new OnClickListener() {
publicvoid onClick(View v) {
setResult(RESULT_OK, new Intent().setAction("ok!"));
//要调用finish()销毁当前的Activity,否则无法返回到原来的Activity
finish();
}
};
private OnClickListener failListener = new OnClickListener() {
publicvoid onClick(View v) {
setResult(RESULT_OK, new Intent().setAction("fail!"));
//要调用finish()销毁当前的Activity,否则无法返回到原来的Activity
finish();
}
};
}
3. 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: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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="@string/go" />
</RelativeLayout>
4. receiver_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: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=".MainActivity" >
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="@string/ok" />
<Button
android:id="@+id/fail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ok"
android:layout_below="@+id/ok"
android:layout_marginTop="38dp"
android:text="@string/fail" />
</RelativeLayout>
二、 运行结果
三、 总结
1) Activity跳转,无返回结果。从一个Activity启动另一个Activity,直接startActivity(new Intent(当前Activity.this, 下一Activity.class))。
2) Activity跳转,有返回结果。
startActivityForResult(Intenet intenet,int requestCode)。
3) Activity传送数据:
intent.putExtra("Text", mText);
Bundle extras = getIntent().getExtras();
String text = extras.getString("Text");