Arun
Arun

Reputation: 168

SharedPreferences data not deleting

I am trying to delete data from android shared preference, but it is not deleting.

here is my code -

To save data in shared preference -

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = sp.edit();
                editor.putString(PREFIX + mAppWidgetId, item.getId());
                editor.commit()

Code to get data from shared preference -

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        String id = sp.getString(ConfigurableWidgetActivity.PREFIX + appWidgetId, null);

Code to delete shared preference -

List<String> deletedWidgetIds = new ArrayList<>();
        if (appWidgetIds != null && appWidgetIds.length > 0) {
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
            Map<String, ?> data =  sp.getAll();
            for(String key : data.keySet()) {
                boolean isFound = false;
                for (int widgetId : appWidgetIds) {
                    if (key.equals(ConfigurableWidgetActivity.PREFIX + widgetId)) {
                        isFound = true;
                        break;
                    }
                }
                if (!isFound) {
                    deletedWidgetIds.add(key);
                }
            }
        }

        if (deletedWidgetIds.isEmpty()){
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
            sp.edit().clear();
            sp.edit().apply();
        } else {
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
            for (String ids : deletedWidgetIds) {
                sp.edit().remove(ids);
                sp.edit().apply();
            }
        }

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        for (String key : sp.getAll().keySet()) {
            Log.d(TAG, "keys " + key);
        }

I am able to add and read/get data from shared preference but can't delete data from shared preference.

Upvotes: 0

Views: 939

Answers (2)

Aalap Patel
Aalap Patel

Reputation: 2076

I see your code that you use apply() after removing data, I suggest using commit() instead. See this docs https://www.codeday.top/2017/07/26/31306.html

So, commit() is synchronous and it shows whether it stored value or failed with respective return value true or false so you can handle UI and/or logic in both the case, whereas, apply() is asynchronous and doesn't have any return type. Perhaps you don't know apply() is storing or not use commit().

if (deletedWidgetIds.isEmpty()){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        sp.edit().clear();
        boolean isCleared = sp.edit().commit();

        if(isCleared)
           Log.d(TAG, "Cleared");
        else
           Log.d(TAG, "Not Cleared");

    } else {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        for (String ids : deletedWidgetIds) {
            sp.edit().remove(ids);

        boolean isCleared = sp.edit().commit();

        if(isCleared)
           Log.d(TAG, "Cleared");
        else
           Log.d(TAG, "Not Cleared");
        }
    }

Upvotes: 1

John O&#39;Reilly
John O&#39;Reilly

Reputation: 10350

You need to chain request together:

sp.edit().remove(ids).apply();

Alternatively you can do something lie:

editor = sp.edit();
editor.remove(ids);
editor.apply()

Upvotes: 2

Related Questions