【问题标题】:Shared Preferences lost at reboot on ICS在 ICS 上重新启动时共享首选项丢失
【发布时间】:2012-12-02 19:32:56
【问题描述】:

!!请帮助,我的用户因此丢失数据,我不知道该怎么办。

这种情况只发生在冰淇淋三明治上,在果冻豆、Hoenycomb 上效果很好,这是什么原因造成的?

由于我的一个字符串只是一个数字,将它保存为浮点数或整数会更好吗?

奇怪的是,它在我的带有 android 4.0.3 的 acer a500 平板电脑上运行良好,但在 4.0.3 的模拟器上却无法运行,我收到了一位使用 Galaxy s3 的用户的投诉在 4.0.4 上,对他来说也没有工作..

我将两个字符串保存到共享首选项中,如下所示:

谢谢

    private static final String PREFS_NAME = "com.MY_PACKAGE.MY_APP.MY_WIDGET_PROVIDER_CLASS";
    private static final String PREF_PREFIX_KEY = "prefix_"; 
    private static final String PREF_SIZE_PREFIX_KEY = "prefix_";

...

  static void saveTitlePref(Context context, int mAppWidgetId, String text) {

        SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();
        editor.putString(PREF_PREFIX_KEY + mAppWidgetId, text);
        editor.commit();
    }

   static void saveSizePref(Context context, int mAppWidgetId, String size) {

        SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();
        editor.putString(PREF_SIZE_PREFIX_KEY, size);
        editor.commit();
    }


   static String loadTitlePref(Context context, int mAppWidgetId) {

        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        String prefix = prefs.getString(PREF_PREFIX_KEY + mAppWidgetId, null);
        // If there is no preference saved, get the default from a resource
        if (prefix != null) {
            return prefix;
        } else {
            return context.getString(R.string.appwidget_prefix_default);
        }
    }

    static String loadSizePref(Context context, int mAppWidgetId) {

        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        String sizeprefix = prefs.getString(PREF_SIZE_PREFIX_KEY, null);
        // If there is no preference saved, get the default from a resource
        if (sizeprefix != null) {
            return sizeprefix;
        } else {
            return "24";
        }
    }

字符串 xml

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

    <string name="appwidget_prefix_default"></string>
    <string name="appwidget_text_format"><xliff:g id="prefix">%1$s</xliff:g></string> 
    <string name="appwidget_size_format"><xliff:g id="sizeprefix">%s</xliff:g></string>


</resources>

【问题讨论】:

  • 好奇:为什么在返回之前检查prefixsizeprefix 是否为空?您不想检查它们是否为空吗?
  • 我在 google 的一个 api demo 之上构建了我的应用程序,我是初学者,所以我不知道

标签: android sharedpreferences android-4.0-ice-cream-sandwich


【解决方案1】:

首先你不应该相信模拟器上发生的事情,因为它有时会奇怪地工作。

您的代码看起来不错。

我会说您应该在其他设备上检查相同的问题并检查一切是否正常,因为您的用户很可能遇到其他问题,最终推断出事情不正常有偏好

我自己经历过一些情况,用户说应用程序不适合他们,即使我在他们使用的同一设备上测试过它。

【讨论】:

  • 好的,谢谢,我会试着找人用 Galaxy S3 4.0.4 试试看。
  • 如果锻炼正常,别忘了接受答案 ;-) :-)
【解决方案2】:

您对每个小部件的尺寸偏好不是唯一的,这是故意的吗?

你做的

editor.putString(PREF_SIZE_PREFIX_KEY, size);

而不是

editor.putString(PREF_SIZE_PREFIX_KEY + appWidgetId, size);

与检索相同:

 String sizeprefix = prefs.getString(PREF_SIZE_PREFIX_KEY, null);

因此,对 this 的每次新调用都会为其他小部件覆盖它。

【讨论】:

  • 谢谢,但这是故意的,因为该字符串用于更改 textview 的文本大小,并且由于某种原因,当我将 widgetId 附加到它时,文本大小不会改变.. (我刚刚又试了一次)
  • 这没有发生,有多个共享的大小偏好没有问题
  • 好的,现在用 appwidgetid 修复了它,但这并不能解决问题..
猜你喜欢
  • 2012-11-17
  • 2011-12-18
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多