WeakReferences
)Serializable
: it‘s error prone and horribly slow. So in general: stay away from Serializable
if possible.(4)通过intent传递数据是有大小限制滴,超过限制,要么抛异常,要么新的Activity启动失败,所以还是很严重的啊,可参见下一篇博客说明。
FLAG_ACTIVITY_CLEAR_TASK
或者 BroadcastReceiver 杀掉其他的activity。
public class DataHolder {
private String data;
public String getData() {return data;}
public void setData(String data) {this.data = data;}
private static final DataHolder holder = new DataHolder();
public static DataHolder getInstance() {return holder;}
}
DataHolder.getInstance().setData(data);
String data = DataHolder.getInstance().getData();
public class DataHolder {
private static String data;
public static String getData() {return data;}
public static String setData(String data) {this.data = data;}
}
启动之前设置数据,新的activity获取数据。public class DataHolder {
Map<String, WeakReference<Object>> data = new HashMap<String, WeakReference<Object>>();
void save(String id, Object object) {
data.put(id, new WeakReference<Object>(object));
}
Object retrieve(String id) {
WeakReference<Object> objectWeakReference = data.get(id);
return objectWeakReference.get();
}
}
启动之前:DataHolder.getInstance().save(someId, someObject);
新activity中:DataHolder.getInstance().retrieve(someId);
这里可能需要通过intent传递id,如果数据唯一,id都可以不传递的。save() retrieve()中id都固定即可。原文:http://www.cnblogs.com/penghuster/p/5328045.html