How to delete saved data in Shared Preferences in android?

13,858

Solution 1

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();

Solution 2

I have used in my project it's working perfect..

Preferences = getSharedPreferences("here is your preferences name", Context.MODE_PRIVATE);

  1. editor = preferences.edit();
  2. editor.clear();
  3. editor.commit();
Share:
13,858
vinothp
Author by

vinothp

Do what you like. Believe in what you do. Young and energetic individual with great passion on Mobile Application Development. Currently working as a full time mobile application developer for Android and iOS. In free time working on personal projects as well. My First Personal App - Location Plotter/ House Viewing

Updated on June 14, 2022

Comments

  • vinothp
    vinothp about 2 years

    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

  • vinothp
    vinothp almost 12 years
    Yeah i am checking that condition too.. The condition satisfied.. what it does is its loading the login page after few seconds it shows posted on wall..