[SOLVED] How to delete saved data in Shared Preferences in android?

Issue

I am using facebook api in my app. Its working fine i can login and post on wall. But i couldn’t delete the login information.

This is the code

   public boolean saveCredentials(Facebook facebook) {
        Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(TOKEN, facebook.getAccessToken());
        editor.putLong(EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    }

    public boolean restoreCredentials(Facebook facebook) {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
        return facebook.isSessionValid();
    }

    public boolean removeCredentials()
    {
        SharedPreferences prefs = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);

            facebook.setAccessToken(prefs.getString("", null));
        facebook.setAccessExpires(prefs.getLong("", 0));
        Editor editor = prefs.edit(); 
        editor.clear();
        editor.commit(); 
        return true; 
    }

The Shared preferences details hasn’t deleted by calling removeCredentials() method. It just post the message on facebook wall.

I just want to delete the saved details and if again user requests to posting the message on wall then i need to popup the login screen.

Thanks for your Help guys

Solution

Refer below link

https://stackoverflow.com/a/3687333/1441666

SharedPreferences.Editor.remove() followed by a commit()

or

SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().remove("text").commit();

Answered By – Nirali

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.