3

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
  • 3
  • 15
vinothp
  • 9,939
  • 19
  • 61
  • 103

4 Answers4

12

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,571
  • 6
  • 40
  • 53
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();
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,609
  • 9
  • 103
  • 153
  • 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
0
val preferences = getSharedPreferences(AppConstants.TAG, Context.MODE_PRIVATE);
preferences.edit().remove(AppConstants.USER_RESPONSE).commit();
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jun 16 '22 at 17:36
  • @JeremyCaney thanks for your feedback sir. i will keep in mind for next time – Abhay kumar bhumihar Jun 17 '22 at 12:04
  • Why not just [edit] your answer this time, to improve its value to the community and future readers? – Jeremy Caney Jun 18 '22 at 05:35