通过 Activity 中的 Intent 传递不同的数据

1.传递整数数据:

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);

ReceiverActivity

Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0); // set 0 as the default value if no value for intVariableName found

2.传递双重数据:

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("doubleVariableName", doubleValue);
startActivity(myIntent);

ReceiverActivity

Intent mIntent = getIntent();
double doubleValue = mIntent.getDoubleExtra("doubleVariableName", 0.00); // set 0.00 as the default value if no value for doubleVariableName found

3.传递字符串数据:

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("stringVariableName", stringValue);
startActivity(myIntent);

ReceiverActivity

Intent mIntent = getIntent();
String stringValue = mIntent.getExtras().getString("stringVariableName");

要么

Intent mIntent = getIntent();
String stringValue = mIntent.getStringExtra("stringVariableName");

4.传递 ArrayList 数据:

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putStringArrayListExtra("arrayListVariableName", arrayList);
startActivity(myIntent);

ReceiverActivity

Intent mIntent = getIntent();
arrayList = mIntent.getStringArrayListExtra("arrayListVariableName");

5.传递对象数据:

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("ObjectVariableName", yourObject);
startActivity(myIntent);

ReceiverActivity

Intent mIntent = getIntent();
yourObj = mIntent.getSerializableExtra("ObjectVariableName");

注意: 请记住,你的自定义类必须实现 Serializable 接口。

6.传递 HashMap <String,String>数据:

SenderActivity

HashMap <String,String> hashMap;

Intent mIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
mIntent.putExtra("hashMap", hashMap);
startActivity(mIntent);

ReceiverActivity

Intent mIntent = getIntent();    
HashMap<String, String> hashMap = (HashMap<String, String>)
mIntent.getSerializableExtra("hashMap");

7.传递位图数据:

SenderActivity

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("image",bitmap);
startActivity(mIntent);

ReceiverActivity

Intent mIntent = getIntent();
Bitmap bitmap = mIntent.getParcelableExtra("image");