Here is my code:
mPrefs = getSharedPreferences("facebook_session",MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if(access_token != null) {
facebook.setAccessToken(access_token);
Log.i("access_token","facebook.setAccessToken(access_token), and access_token=="+access_token);
}
if(expires != 0) {
Log.i("expires","facebook.setAccessToken(access_token), and expires=="+expires);
facebook.setAccessExpires(expires);
}
/*
* Only call authorize if the access_token has expired.
*/
if(!facebook.isSessionValid()) {
Log.v("in !facebook.isSessionValid()", "no pref");
facebook.authorize( this, new String[] { "email", "publish_checkins" }, new DialogListener(){
@Override
public void onComplete(Bundle values) {
Log.v("facebook.authorize", "no pref");
Log.v("postToWall","mFacebook.getAccessToken(): "+facebook.getAccessToken());
Log.v("postToWall","mFacebook.getAccessExpires(): "+facebook.getAccessExpires());
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
@Override
public void onFacebookError(FacebookError e) {
Log.v("facebook.authorize","mFacebook.onFacebookError(): "+e);
}
@Override
public void onError(DialogError e) {
Log.v("facebook.authorize","mFacebook.onFacebookError(): "+e);
Log.v("facebook.authorize","mFacebook.onError(): "+facebook.getAccessToken());
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),
"You must be registered and signed in to perform that action",
Toast.LENGTH_LONG).show();
finish();
// onCancel, leaving the app
}
});
}//end of pref check
and in the logout function, I tried to logout form facebook and clear the sharedPreferences as well:
public void Logout(View view){
String access_token = mPrefs.getString("access_token", null);
Log.i("access_token","mPrefs.getString(access_token, null), and access_token=="+access_token);
SharedPreferences.Editor editor = mPrefs.edit();
editor.remove("access_token");
editor.remove("access_expires");
editor.commit();
Log.i("access_token","mPrefs.getString(access_token, null), and access_token=="+access_token);
mAsyncRunner = new AsyncFacebookRunner(facebook); // initialize AsyncRunner, make sure it's not null
mAsyncRunner.logout(Login.this, new RequestListener() {
@Override
public void onComplete(String response, Object state) {
finish();
}
@Override
public void onIOException(IOException e, Object state) {}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {}
@Override
public void onFacebookError(FacebookError e, Object state) {}
});
}
So, here is the question: No matter how hard I try, the values stored in sharedPreferences didn’t change. I tried clear(), remove(), commit(), apply() etc…..
Please help!
If you are using this
Logstatement to verify whether the values stored inSharedPreferencesare cleared or not, you are not doing it correctly.Your value
access_tokenwas assigned a value before you removed it, so when you reference the variable it will still retain that value. To correctly test whether the value has been removed fromSharedPreferences, you need to try accessing that value again after you’ve removed it by doing the following after youreditor.commit()command: