2

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

Zamani
  • 306
  • 2
  • 15
vinothp
  • 9,760
  • 18
  • 59
  • 102

3 Answers3

11

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();
Community
  • 1
  • 1
Nirali
  • 13,013
  • 6
  • 38
  • 52
1

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();
smit modi
  • 171
  • 1
  • 4
0

The problem here is (IMO) you clear credentials from SharedPreferences, which removes your Token and Date form SharePreferences only.

But you don't nullify your facebook object's session, Thats why, for now your facebook has Token and Date in its field variables, and it is able to post that message.

Because

  • when facebook object is created, it automatically get those credentials from SharedPreferences.
  • then you remove the credentials
  • but facebook object has credentials, try to nullify them too.

Update: So when you have nullified them, always check the session before posting any activity to facebook, like this:

facebook.isSessionValid(); //checks if the session valid
Adil Soomro
  • 37,173
  • 9
  • 101
  • 148
  • 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.. – vinothp Jul 13 '12 at 12:44