62,614
社区成员
发帖
与我相关
我的任务
分享
//调用
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;
}
}
public class AvcSignElectronDialog extends Dialog {
private static AvcSignElectronDialog mSingleton = null;
private static int mCountSingleton = 0;
public static synchronized AvcSignElectronDialog getInstance(Context context, int theme) {
mCountSingleton++;
if (mSingleton == null) {
mSingleton = new AvcSignElectronDialog(context, theme);
}
return mSingleton;
}
public static synchronized void clearInstance() {
mCountSingleton--;
if ((mCountSingleton==0) && (mSingleton!=null)) {
mSingleton = null;
}
}
public static synchronized void clearInstance() {
if (mSingleton != null) {
mSingleton = null;
}
}
[/quote]
不计数有可能程序会崩溃
尤其在多线程下,一个线程刚getInstance完,另一个就给clear了,有可能会崩溃
还是别这么写了public static synchronized void clearInstance() {
if (mSingleton != null) {
mSingleton = null;
}
}