用法一:跳转后的activity不需要回传参数
send.xml
receive.xml
Send.java
Receive.java
用法二:跳转后的activity需要回传参数
send.xml
receive.xml
Send.java
Receive1.java
Receive2.java
用法二的结果:
send.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转到另一个Activity"/> </LinearLayout>
receive.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/txt" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
Send.java
package com.example.intentdemo;
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;
public class Send extends Activity {
private Button btn=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Send.this, Receive.class);
intent.putExtra("param", "你好!");
startActivity(intent);
}
});
}
}
Receive.java
package com.example.intentdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Receive extends Activity {
private TextView txt=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receive);
txt=(TextView)findViewById(R.id.txt);
Intent intent=getIntent();
txt.setText(intent.getStringExtra("param"));
}
}
用法二:跳转后的activity需要回传参数
![](https://i-blog.csdnimg.cn/blog_migrate/d0962f2a58b7bc65e4523aa3d08303a2.png)
send.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/txt" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转到Receive1"/> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转到Receive2"/> </LinearLayout>
receive.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/txt" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="回到第一个Activity"/> </LinearLayout>
Send.java
package com.example.intentdemo;
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.TextView;
public class Send extends Activity {
private Button btn1=null;
private Button btn2=null;
private TextView txt=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
txt=(TextView)findViewById(R.id.txt);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Send.this, Receive1.class);
intent.putExtra("param", "这是Receive1");
startActivityForResult(intent, 1);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Send.this, Receive2.class);
intent.putExtra("param", "这是Receive2");
startActivityForResult(intent, 2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK){
switch(requestCode){
case 1:
txt.setText("从Receive1返回数据"+data.getStringExtra("ret"));
break;
case 2:
txt.setText("从Receive2返回数据"+data.getStringExtra("ret"));
break;
default:
break;
}
}
}
}
Receive1.java
package com.example.intentdemo;
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.TextView;
public class Receive1 extends Activity {
private TextView txt=null;
private Button btn=null;
private Intent intent=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receive);
txt=(TextView)findViewById(R.id.txt);
btn=(Button)findViewById(R.id.btn);
intent=getIntent();
txt.setText(intent.getStringExtra("param"));
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("ret", "1");
setResult(RESULT_OK, intent);
finish();
}
});
}
}
Receive2.java
package com.example.intentdemo;
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.TextView;
public class Receive2 extends Activity {
private TextView txt=null;
private Button btn=null;
private Intent intent=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receive);
txt=(TextView)findViewById(R.id.txt);
btn=(Button)findViewById(R.id.btn);
intent=getIntent();
txt.setText(intent.getStringExtra("param"));
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("ret", "2");
setResult(RESULT_OK, intent);
finish();
}
});
}
}
用法二的结果:
![](https://i-blog.csdnimg.cn/blog_migrate/95b0d947bed6c07ddbc843a502713ee7.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c3f40cf8b1fa90159de82ebb94e08fc6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c92be87c3c17a30a33f9ec36deac29b0.png)