回顾这篇文章列举了使用单例的几个问题 并看过使用单例模式的Android应用程序的几个例子,我想知道使用Singletons而不是通过全局应用程序状态共享的单个实例是一个好主意(继承android.os.Application并获取它通过context.getApplication()).
两种机制都有哪些优点/缺点?
说实话,我希望在这篇文章中使用Web应用程序的单例模式得到相同的答案,不是一个好主意!但适用于Android.我对么?DalvikVM有什么不同呢?
编辑:我想对涉及的几个方面有意见:
有很多问题与共享偏好和替代方案有关.我的问题:当我关闭设备或杀死应用程序时,共享首选项会丢失.请注意,我的代码实际上是在使用Acer A500.但在我的摩托罗拉Xoom MZ604上,它无法正常工作!!
首先,我尝试在onCreate中恢复我的HashSet.这个方法是肯定调用的,并以单例形式实现.
public boolean restoreCollection(Context context){
SharedPreferences settings = context.getSharedPreferences(context.getString(R.string.restore_values), 0);
if(settings.getStringSet(context.getString(R.string.collection), null) != null){
collection = settings.getStringSet(context.getString(R.string.collection), null);
return true;
}
collection = new HashSet<String>();
return false;
}
Run Code Online (Sandbox Code Playgroud)
通过调用onDestroy,我保存了HashSet.即使没有给出,这个方法是肯定调用的,但是在任何情况下,首选项都会丢失,我试图将它保存在onPause中,结果相同.
public void saveCollection(Context context){
SharedPreferences settings = context.getSharedPreferences(context.getString(R.string.restore_values), 0);
SharedPreferences.Editor e = settings.edit();
e.putStringSet(context.getString(R.string.collection), collection);
e.commit();
}
Run Code Online (Sandbox Code Playgroud)
共享首选项和XOOM设备是否有任何问题,或者我是唯一的问题?也许我的代码有些可疑但数据不会在我的Acer Tablet上丢失.
我也尝试过PreferenceManager.getDefaultSharedPreferences(context)来获取SharedPreferences的对象
谢谢你的帮助,克里斯
我试图弄清楚当我尝试获取应用程序的共享首选项时,它在启动时发生此错误的原因.代码只是:
settings = this.getSharedPreferences(Globals.PREFS_NAME,0)
看起来像一个腐败,但错误是在应用程序中,因为我已将其安装在多个设备上,并发生相同的错误.我卸载了应用程序并重新安装它无济于事.还做了一个完全干净的构建.我用Google搜索错误,似乎无法找到任何内容.
我以前工作得很好.所以我很难过.任何线索都会非常感激......
问候,
W/ApplicationContext( 1541): getSharedPreferences
W/ApplicationContext( 1541): org.xmlpull.v1.XmlPullParserException: Map value without name attribute: boolean
W/ApplicationContext( 1541): at com.android.internal.util.XmlUtils.readThisMapXml(XmlUtils.java:521)
W/ApplicationContext( 1541): at com.android.internal.util.XmlUtils.readThisValueXml(XmlUtils.java:733)
W/ApplicationContext( 1541): at com.android.internal.util.XmlUtils.readValueXml(XmlUtils.java:667)
W/ApplicationContext( 1541): at com.android.internal.util.XmlUtils.readMapXml(XmlUtils.java:470)
W/ApplicationContext( 1541): at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:376)
W/ApplicationContext( 1541): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
W/ApplicationContext( 1541): at net.robmunro.mypod.WelcomeActivity.onEWCreate(WelcomeActivity.java:160)
W/ApplicationContext( 1541): at net.robmunro.mypod.AbstractEWActivity.onCreate(AbstractEWActivity.java:25)
W/ApplicationContext( 1541): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/ApplicationContext( 1541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
W/ApplicationContext( 1541): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
W/ApplicationContext( 1541): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
W/ApplicationContext( 1541): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
W/ApplicationContext( 1541): at android.os.Handler.dispatchMessage(Handler.java:99)
W/ApplicationContext( 1541): …Run Code Online (Sandbox Code Playgroud)