I have a SettingsActivity
where there are several options including finding Facebook friends and logging out. So when a user chooses to find her Facebook friends, she will be sent to another activity where I let her logging in with her Facebook account and save her access token. And then when she chooses to log out, her access token will be cleared. The problem is, my logout
method is not written in the same activity with the one I created the session, so when I tried this:
Session session = Session.getActiveSession();
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
}
Logcat points NullPointerException
. Then I tried:
Session.setActiveSession(null);
And that doesn't work either (friends of the user logged in earlier are still shown, instead of asking the new user to log in).
So what should I do to clear the obtained token? Thank you in advance.
-EDIT-
I think there's something wrong with the activity which I use to open the session. I followed Facebook's GraphAPISample and this is what I have done:
public class FacebookFriendsActivity extends FragmentActivity{
private BroadcastReceiver broadcast;
public static Session session;
private boolean pendingRequest;
static final String PENDING_REQUEST_BUNDLE_KEY = "PendingRequest";
private DatabaseHandler db;
private FindFacebookFriends task;
private ProgressBar progress;
private TextView text;
private Button retryBtn;
private ListView userLayout;
private ArrayList<User> userList;
private FollowAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook_friends);
// ... set views
session = createSession();
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
showFriends();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (session.onActivityResult(this, requestCode, resultCode, data) &&
pendingRequest &&
session.getState().isOpened()) {
showFriends();
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
pendingRequest = savedInstanceState.getBoolean(PENDING_REQUEST_BUNDLE_KEY, pendingRequest);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(PENDING_REQUEST_BUNDLE_KEY, pendingRequest);
}
private void showFriends(){
task = new FindFacebookFriends();
task.execute(session.getAccessToken());
}
private Session createSession() {
Session activeSession = Session.getActiveSession();
if (activeSession == null || activeSession.getState().isClosed()) {
activeSession = new Session.Builder(this).setApplicationId(getString(R.string.fb_app_id)).build();
Session.setActiveSession(activeSession);
new SaveFacebookId().execute(activeSession.getAccessToken());
}
return activeSession;
}
}
When I try putting a Toast
in createSession()
to check the state of the created activeSession
, it always shows that the session is closed. It's weird because the showFriends()
method is still triggered and even after I delete and install the app again, it still show the same friend list. I'm really confused here.
Do like this
in Facebook SDK v4 we can log out using
note that don't forget to initialize sdk like this
You should pass session variable through Parcelable interface and clear session through that session variable. Pass facebook session like this : Passing a Facebook session across activities
Using
Facebook SDK v4
you can do the following:You can take a look at the
LoginManager
class.Try this below code for session clear in facebook integration android.
Put below method in your activity and call before the login to facebook.
For me it's working... Hope this is helpful for you...