Android备忘录编程之全局备忘录

package com.haitang.dollprint.utils;


import android.util.SparseArray;


/**
 * 全局备忘录类
 * 
 * @author 王永迪 2015.8.28
 */
public class MemoUtils {


public static SparseArray<Object> mMenoObject;


private static MemoUtils sMemoUtils;


public static MemoUtils GetInstance() {
if (sMemoUtils == null) {
synchronized (MemoUtils.class) {
// 如果同时都请求都可以进入第一层判断,但是第二层判断不一定都能通过;
if (sMemoUtils == null) {
sMemoUtils = new MemoUtils();
}
}
}
return sMemoUtils;
}


public void addMeno(Object object, int key) {
if (null == mMenoObject) {
mMenoObject = new SparseArray<Object>();
}
mMenoObject.put(key, object);
}


public <T extends Object> T getMeno(int key) {
if (null != mMenoObject && 0 < mMenoObject.size()) {
Object Object = mMenoObject.get(key);
return (T) Object;
} else {
return null;
}
}
}
//-----------------------------
 //调用方式:这样能保证传入任何对象时都可以将其进行保存,
                MemoUtils.GetInstance().addMeno(mActivity_message_text, 123);
TextView view = (TextView) MemoUtils.GetInstance().getMeno(123);
Utils.LOGE(TAG, "view.getText() = " + view.getText());
MemoUtils.GetInstance().uploadMeno(mReceive_Notice_text, 123);
view = (TextView) MemoUtils.GetInstance().getMeno(123);
Utils.LOGE(TAG, "view.getText() = " + view.getText());
MemoUtils.GetInstance().deleteMeno(123);
view = (TextView) MemoUtils.GetInstance().getMeno(123);
Utils.LOGE(TAG, "view ==null = " + (view ==null));


你可能感兴趣的:(Android备忘录编程之全局备忘录)