当前位置: 代码迷 >> J2SE >> 单实例的类对象使用后,怎么释放
  详细解决方案

单实例的类对象使用后,怎么释放

热度:47   发布时间:2016-04-23 19:53:26.0
单实例的类对象使用后,如何释放?
有个需求,在适当的时候,弹出一个Dialog,请求用户输入签名。
如果不使用单实例,只需要将mDlgSignElectron=null,就能够释放这个对象了。
使用单实例,防止弹出多个Dialog。但同时带来的一个问题,当用户输入签名退出Dialog,这个单实例的对象一直驻留在内存里,如何释放呢?
//调用
AvcSignElectronDialog mDlgSignElectron;
mDlgSignElectron = AvcSignElectronDialog.getInstance(LoginActivity.this, R.style.SignElectronDialogTheme);
mDlgSignElectron.show();

//单实例类
public class AvcSignElectronDialog extends Dialog {
private static AvcSignElectronDialog mSingleton = null;

public static synchronized AvcSignElectronDialog getInstance(Context context, int theme) {  
            if (mSingleton == null) {  
                mSingleton = new AvcSignElectronDialog(context, theme);
            }  
            return mSingleton;
        }

private AvcSignElectronDialog(Context context, int theme) {
super(context, theme);
this.mContext = context;
}
}

------解决思路----------------------
引用:
Quote: 引用:

既然是单例,那么正常情况下是不应该被释放的。

如果要释放,那就要进行获取和放回计数,在计数为0是,释放。
单例,这个计数应该一直为1!


我意思是,你用的时候要getInstance,不用的时候要releaseInstance

这样计数器就有增有减了。
  相关解决方案