Question

!! Please Help, my users are loosing data due to this, and I don't know what to do.

This only happens on Ice Cream Sandwich, works fine on Jelly Bean, Hoenycomb, what causes this?

Since one of my strings is just a number, would it be better to save it as a float or int??

Weird thing is that, it works fine on my acer a500 tablet, with android 4.0.3, but it doesn't work on the emulator with 4.0.3, I've gotten a complaint from a user with a galaxy s3 on 4.0.4, for him is didn't work either..

I'm saving two strings to Shared Preferences like this:

Thanks

    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";
        }
    }

Strings 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>
Was it helpful?

Solution

First of all you should not believe on what is happening on emulator as it works weirdly some times.

Your code seems fine.

I would say that you should check the same issue on other devices and check if things work fine, because it is quite possible that there is some other issue that one of your user experienced, which ultimately deduced that things are not working fine with preferences

I have myself experienced some cases where user says the app does not work for them even though I have tested it on the same device they were using.

OTHER TIPS

Your size preference isn't unique per widget is this intentional?

Your doing

editor.putString(PREF_SIZE_PREFIX_KEY, size);

instead of

editor.putString(PREF_SIZE_PREFIX_KEY + appWidgetId, size);

Same with retrieval:

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

Therefore every new call to this will override it for other widgets.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top