Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SharedPreferences not saving

I'm accessing SharedPreferences inside my AppWidgetProvider subclass as follow:

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.d( TAG, "WidgetProvider.onReceive " + intent.getAction() );
        Toast.makeText( context, "WidgetProvider.onReceive " + intent.getAction(), Toast.LENGTH_LONG ); // TOAST NOT WORKING

        Bundle pExtra = intent.getExtras();
        if ( pExtra != null ) {
            SharedPreferences pref = context.getApplicationContext().getSharedPreferences("widget_pref", Context.MODE_PRIVATE );
            int iWidgetId = intent.getExtras().getInt("widgetId");
            int iCount = pref.getInt("widgetCount", 0);
                    // HERE IS THE PROBLEM, iCount will always start from 0!
            for ( int i = 0; i < 10; i++ ) {
                iCount = pref.getInt("widgetCount", 0);
                iCount++;
                pref.edit().putInt("widgetCount", iCount);
                boolean bSaved = pref.edit().commit(); // return TRUE
                bSaved = pref.contains("widgetCount"); // return FALSE
                Log.w(TAG, "WidgetProvider.onReceive: " + iWidgetId + " > " + iCount + " result: " + bSaved);
            }

                    // non-relevant code
            RemoteViews remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget_layout );
            remoteViews.setTextViewText( R.id.hellotextview, "ID: " + iWidgetId + " > " + iCount );
            Intent clickIntent = new Intent( context, WidgetProvider.class );
            clickIntent.setAction("WIDGET_PROVIDER_CLICKED");
            clickIntent.putExtra("widgetId", iWidgetId);
            PendingIntent clickPendingIntent = PendingIntent.getBroadcast( context, 0, clickIntent, 0 );
            remoteViews.setOnClickPendingIntent( R.id.hellolayout, clickPendingIntent );
//          AppWidgetManager.getInstance(context).updateAppWidget( iWidgetId, remoteViews );
//          ComponentName name = new ComponentName(context, WidgetProvider.class);
            ComponentName name = new ComponentName(context.getApplicationContext(), WidgetProvider.class);
            AppWidgetManager.getInstance(context).updateAppWidget(name, remoteViews);
        }
    }

However, as you can see from the comment, even though I am looping and saving the iCount will always start from default value again. The contains() even return false even though I just commit. I've tried different way of getting the SharedPreferences (using the context directly, or context.getApplicationContext() as well as different Context.MODE_XXX)

Thanks in advance!

like image 808
Jenny Kim Avatar asked Sep 24 '26 11:09

Jenny Kim


2 Answers

You are creating a new SharedPreferences.Editor object every time by calling pref.edit(). Cache the SharedPreferences.Editor object before entering the for loop and re-use that instance.

like image 84
Zennichimaro Avatar answered Sep 26 '26 01:09

Zennichimaro


Edit your Code Like:

I have declared SharedPreferences.Editor object before for loop and then reuse in loop:

Try it:

SharedPreferences pref = context.getApplicationContext().getSharedPreferences("widget_pref", Context.MODE_PRIVATE );
int iWidgetId = intent.getExtras().getInt("widgetId");
int iCount = pref.getInt("widgetCount", 0);

/**** ADD THIS LINE ****/
SharedPreferences.Editor prefEditor = pref.edit();


// HERE IS THE PROBLEM, iCount will always start from 0!
for ( int i = 0; i < 10; i++ ) {
    iCount = pref.getInt("widgetCount", 0);
    iCount++;

    /** Edit Here Like **/
    prefEditor.putInt("widgetCount", iCount);
    prefEditor.commit();

    //boolean bSaved = prefEditor.commit(); // return TRUE
    bSaved = pref.contains("widgetCount"); // return FALSE

    Log.w(TAG, "WidgetProvider.onReceive: " + bSaved);
}

Comment if you any prob.

like image 20
Pratik Butani Avatar answered Sep 26 '26 00:09

Pratik Butani