Intent intent can be used for data transfer between activity, and can generally be divided into the following two cases, from the current activity to the target activity has no return value: 1. No return value after delivery:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
在起始Activity中,发送数据
protected
void
onCreate(Bundle saveInstanceState){
super
.onCreate(saveInstanceState);
setContentView(R.layout.thisactivity);
Intent intent =
new
Intent();
//设置起始Activity和目标Activity,表示数据从这个Activity传到下个Activity
intent.setClass(ThisActivity.
this
,TargetActivity.
class
);
//绑定数据
intent.putExtra(
"username"
,username);
//也可以绑定数组
intent.putExtra(
"userpass"
,userpass);
//打开目标Activity
startActivity(intent);
}
在目标Activity中,接收数据:
protected
void
onCreate(Bundle saveInstanceState){
super
.onCreate(saveInstanceState);
setContentView(R.layout.targetactivity);
//获得意图
Intent intent = getIntent();
//读取数据
String name = intent.getStringExtra(
"username"
);
String pass = intent.getStringExtra("userpass);
}
也可以适用Bundle(捆)
在起始Activity中,发送数据:
protected
void
onCreate(Bundle saveInstanceState){
super
.onCreate(saveInstanceState);
setContentView(R.layout.thisactivity);
Intent intent =
new
Intent();
//设置起始Activity和目标Activity,表示数据从这个Activity传到下个Activity
intent.setClass(ThisActivity.
this
,TargetActivity.
class
);
//一次绑定多个数据
Bundle bundle =
new
Bundle();
bundle.putString(
"username"
,username);
bundle.putString(
"userpass"
,userpass);
intent.putExtras(bundle);
//打开目标Activity
startActivity(intent);
}
在目标Activity中,接收数据:
protected
void
onCreate(Bundle saveInstanceState){
super
.onCreate(saveInstanceState);
setContentView(R.layout.targetactivity);
//获得意图
Intent intent = getIntent();
//读取数据
Bundle bundle = intent.getExtras();
String name = bundle.getString(
"username"
);
String pass = bundle.getString(
"userpass"
);
}
|
2. Return value after delivery: When you need to transfer data from the target activity to the original activity, you can use the above method to define a new Intent to pass the data, or you can use Startactivityforresult (Intent Intent, int requestcode); method.
1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
在起始Activity中,发送数据:
protected
void onCreate(Bundle saveInstanceState){
super
.onCreate(saveInstanceState);
setContentView(R.layout.thisactivity);
Intent intent =
new
Intent();
//设置起始Activity和目标Activity,表示数据从这个Activity传到下个Activity
intent.setClass(ThisActivity.
this
,TargetActivity.
class
);
//绑定数据
intent.putExtra(
"username"
,username);
//也可以绑定数组
intent.putExtra(
"userpass"
,userpass);
//打开目标Activity
startActivityForResult(intent,
1
);
}
//需要重写onActivityResult方法
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent intent){
super
.onActivityResult(requestCode,resultCode,intent);
//判断结果码是否与回传的结果码相同
if
(resultCode ==
1
){
//获取回传数据
String name = intent.getStringExtra(
"name"
);
String pass = intent.getStringExtra("pass);
//对数据进行操作
......
}
在目标Activity中,接收数据:
protected
void onCreate(Bundle saveInstanceState){
super
.onCreate(saveInstanceState);
setContentView(R.layout.targetactivity);
//获得意图
Intent intent = getIntent();
//读取数据
String name = intent.getStringExtra(
"username"
);
String pass = intent.getStringExtra("userpass);
//从EditText中获取新的数据给name和pass
name = editText1.getText().toString();
pass = editText2.getText().toString()
//数据发生改变,需要把改变后的值传递回原来的Activity
intent.putExtra(
"name"
,name);
intent.putExtra(
"pass"
,pass);
//setResult(int resultCode,Intent intent)方法
setResult(
1
,intent);
//销毁此Activity,摧毁此Activity后将自动回到上一个Activity
finish();
}
|
Transfer of data between-activity and intent data transfer