1. 主叫Activity向被叫Activity传递数据:
(1)主叫Activity写数据:
Intent intent = new Intent(action);
intent.putExtra("key", "value");
startActivity(intent);
intent.putExtra("key", "value");
startActivity(intent);
(2)被叫Activity读数据:
Intent intent = getIntent();
String strFromCaller = intent.getStringExtra("key");
2. 被叫Activity向主叫Activity放回数据:
(1)主叫Activity以startActivityForResult()方式启动被叫Activity:
Intent intent = new Intent();
intent.setClass(getApplicationContext(), NewAcitvity.class);
// START_NEWACTIVITY is a predefined constant to identify the Activity invoked by this intent.
startActivityForResult(intent, START_NEWACTIVITY);
(2)被叫Activity写数据:
Uri uri = Uri.parse("back info");
Intent data = new Intent(null, uri);
data.putExtra("key", "value");
setResult(Activity.RESULT_OK, data);
(3)主叫Activity读数据:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case START_NEWACTIVITY:
if (resultCode == Activity.RESULT_OK)
{
Toast.makeText(this, data.getStringExtra("key"), Toast.LENGTH_LONG).show();
// Toast.makeText(this, data.getData().toString(), Toast.LENGTH_LONG).show();
}
break;
}
}