轉載:大飛 http://blog.csdn.net/rflyee/article/details/47431633html
WeakReferences
)
Serializable
: it's error prone and horribly slow. So in general: stay away from
Serializable
if possible.
(4)經過intent傳遞數據是有大小限制滴,超過限制,要麼拋異常,要麼新的Activity啓動失敗,因此仍是很嚴重的啊,可參見下一篇博客說明。android
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都固定便可。