1.传递整数数据:
发件人活动
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class); myIntent.putExtra("intVariableName", intValue); startActivity(myIntent);
接收者活动
Intent mIntent = getIntent(); int intValue = mIntent.getIntExtra("intVariableName", 0); // 如果找不到intVariableName的值,则将0设置为默认值
2.传递双重数据:
发件人活动
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class); myIntent.putExtra("doubleVariableName", doubleValue); startActivity(myIntent);
接收者活动
Intent mIntent = getIntent(); double doubleValue = mIntent.getDoubleExtra("doubleVariableName", 0.00); // 如果找不到doubleVariableName的值,则将0.00设置为默认值
3.传递字符串数据:
发件人活动
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class); myIntent.putExtra("stringVariableName", stringValue); startActivity(myIntent);
接收者活动
Intent mIntent = getIntent(); String stringValue = mIntent.getExtras().getString("stringVariableName");
要么
Intent mIntent = getIntent(); String stringValue = mIntent.getStringExtra("stringVariableName");
4.传递ArrayList数据:
发件人活动
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class); myIntent.putStringArrayListExtra("arrayListVariableName", arrayList); startActivity(myIntent);
接收者活动
Intent mIntent = getIntent(); arrayList = mIntent.getStringArrayListExtra("arrayListVariableName");
5.传递对象数据:
发件人活动
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class); myIntent.putExtra("ObjectVariableName", yourObject); startActivity(myIntent);
接收者活动
Intent mIntent = getIntent(); yourObj = mIntent.getSerializableExtra("ObjectVariableName");
注意:请记住,自定义类必须实现该Serializable接口。
6.传递HashMap <String,String>数据:
发件人活动
HashMap <String,String> hashMap;
Intent mIntent = new Intent(SenderActivity.this, ReceiverActivity.class); mIntent.putExtra("hashMap", hashMap); startActivity(mIntent);
接收者活动
Intent mIntent = getIntent(); HashMap<String, String> hashMap = (HashMap<String, String>) mIntent.getSerializableExtra("hashMap");
7.传递位图数据:
发件人活动
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class); myIntent.putExtra("image",bitmap); startActivity(mIntent);
接收者活动
Intent mIntent = getIntent(); Bitmap bitmap = mIntent.getParcelableExtra("image");