I’m trying to do a simple Facebook Login in my app, but I got some questions about SharedPreferences.
The idea is the App starts and goes to Activity A, it sees if you’re logged in, if you aren’t then it sends you to Activity B, you login and go back to A.
What’s happening is that when I log in at B, it sends me back to A, but A doesn’t appear to load my SharedPreferences and send me back to B, and I got into a loop.
That’s my code in A.
public static String access_token = null;
long expires;
…
private void SharedP() {
// TODO Auto-generated method stub
prefs = getSharedPreferences(access_token, MODE_PRIVATE);
access_token = prefs.getString("access_token", null);
expires = prefs.getLong("access_expires", 0);
if (access_token == null && expires == 0) { //If it's not logged...
Intent login = new Intent("android.intent.action.FACEBOOKLOGIN");
startActivity(login);
}
}
What am I doing wrong? How should I do it?
My code in B:
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Editor edit = fbSP.edit();
edit.putString("access_token", fb.getAccessToken());
edit.putLong("access_expires", fb.getAccessExpires());
edit.commit();
UpdateLoginImage();
BackToA();
}
It seems to me that you are not reading the same shared preferences on A and B.
getSharedPreferences(access_token, MODE_PRIVATE)opens an “access_token” shared preferences file, you should use a file name likegetSharedPreferences("facebook_data", MODE_PRIVATE).Then in activity B, if you follow the Facebook guide on login for android, it uses the activity sharedPreferences
getPreferences(MODE_PRIVATE). This second one returns a preferences file only accesible for the activity B. I’m just guessing this because i don’t have your code, maybe you got that ok.Check if you are opening the same sharedPreferences file on both activities.