一、List中包含简单类型
如果传递List<String>或List<Integer>,可直接使用API,如下:
intent.putStringArrayListExtra(name, value)
intent.putIntegerArrayListExtra(name, value)
二、List中是复杂对象
1、对象实现了Serializable接口,如下:
intent.putExtra(key, List<Object>); 源码如下
/**
* Add extended data to the intent. The name must include a package
* prefix, for example the app com.android.contacts would use names
* like "com.android.contacts.ShowAll".
*
* @param name The name of the extra data, with package prefix.
* @param value The Serializable data value.
*
* @return Returns the same Intent object, for chaining multiple calls
* into a single statement.
*
* @see #putExtras
* @see #removeExtra
* @see #getSerializableExtra(String)
*/
public Intent putExtra(String name, Serializable value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putSerializable(name, value);
return this;
}
获取:
intent.getSerializableExtra(key); 源码如下
/**
* Retrieve extended data from the intent.
*
* @param name The name of the desired item.
*
* @return the value of an item that previously added with putExtra()
* or null if no Serializable value was found.
*
* @see #putExtra(String, Serializable)
*/
public Serializable getSerializableExtra(String name) {
return mExtras == null ? null : mExtras.getSerializable(name);
}
1> 直接传递
intent.putParcelableArrayListExtra(key, ArrayList<Object>); 源码如下
/**
* Add extended data to the intent. The name must include a package
* prefix, for example the app com.android.contacts would use names
* like "com.android.contacts.ShowAll".
*
* @param name The name of the extra data, with package prefix.
* @param value The ArrayList<Parcelable> data value.
*
* @return Returns the same Intent object, for chaining multiple calls
* into a single statement.
*
* @see #putExtras
* @see #removeExtra
* @see #getParcelableArrayListExtra(String)
*/
public Intent putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putParcelableArrayList(name, value);
return this;
}
获取:
intent.getParcelableArrayListExtra(key); 源码如下
/**
* Retrieve extended data from the intent.
*
* @param name The name of the desired item.
*
* @return the value of an item that previously added with putExtra()
* or null if no ArrayList<Parcelable> value was found.
*
* @see #putParcelableArrayListExtra(String, ArrayList)
*/
public <T extends Parcelable> ArrayList<T> getParcelableArrayListExtra(String name) {
return mExtras == null ? null : mExtras.<T>getParcelableArrayList(name);
}
2> 使用Bundle
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(key, List<Object>);
intent.putExtra(key, bundle);
获取:
Bundle bundle = intent.getBundleExtra(key);
List<Object> list = bundle.getParcelableArrayList(key);
1、对象实现了Serializable接口
Bundle.putSerializable(Key,Object);
2、对象实现了Parcelable接口
Bundle.putParcelable(Key, Object);