Facebook SDK 4 for Android - how to log out programmatically

franco phong picture franco phong · Mar 27, 2015 · Viewed 90.7k times · Source

Recently, Facebook released SDK 4 with new and cool updates. I tried to switch into SDK4 to use new features, however, I am struggling with the Login feature of Facebook.

So far, to log out Facebook programmatically, I used :

Session session = Session.getActiveSession();
session.closeAndClearTokenInformation();

But SDK4 seems not to support Session anymore, and in official docs, they mention:

There are two ways to implement Facebook login on Android:

LoginButton class - Which provides a button you can add to your UI. It follows the current access token and can log people in and out.

Well, seems there's no way to log out Facebook programmatically except using LoginButton. Anyone have any idea, please share it here.

Answer

stackex picture stackex · Apr 10, 2015

You can use LoginManager.getInstance().logOut();, even if you use LoginButton because

This UI element wraps functionality available in the LoginManager.

EDIT: Just to mention that this works for Facebook SDK v4. I don't know if they will change it in the future.

@as batoutofhell mention, don't forget to put FacebookSdk.sdkInitialize(getApplicationContext()); to initialize the facebook sdk. Please see here for the details.