Facebook SDK 4 for Android – how to log out programmatically

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 :

  • List Item (a view) order changes unexpextedly while (fast) scrolling in a ListView
  • Lollipop optimized app compiling on Kitkat - LayoutInflaterCompatHC NoClassDefFoundError
  • My debug AndroidManifest.xml is giving me “cannot resolve symbol errors”
  • Detect USB tethering on android
  • How can I script genymotion emulator to launch a given avd, headless?
  • Unit Testing with Firebase
  • 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.

    Related posts:

    Running Android apps on web browser
    Tabs + ViewPager + FragmentStatePagerAdapter - How to remove fragment?
    Android detect Bluetooth disconnect immediately
    Android Database Transaction
    Change the summary of a ListPreference with the new value (Android)
    Android SQLite Query, Insert, Update, Delete, Always Need to be On Background Thread?
  • Prevent ProgressDialog from being dismissed when I click the search button (Android)
  • Is it possible to get a byte buffer directly from an audio asset in OpenSL ES (for Android)?
  • android local permission Androidmanifest SMS READ
  • Android: Update ListView Items every 1 minute
  • Open conversation in Whatsapp and populate the text
  • How to disable WIFI Auto-connect in Android
  • 5 Solutions collect form web for “Facebook SDK 4 for Android – how to log out programmatically”

    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.

    SDK4, if you want to completely de-couple, make sure you also remove the app from the user’s facebook account. This method disconnects the user completely:

    public void disconnectFromFacebook() {
    
        if (AccessToken.getCurrentAccessToken() == null) {
            return; // already logged out
        }
    
        new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
                .Callback() {
            @Override
            public void onCompleted(GraphResponse graphResponse) {
    
                LoginManager.getInstance().logOut();
    
            }
        }).executeAsync();
    }
    

    To handle it with the loginButton:

    //Check if user is currently logged in
            if (AccessToken.getCurrentAccessToken() != null && com.facebook.Profile.getCurrentProfile() != null){
                //Logged in so show the login button
                fbLogin.setVisibility(View.VISIBLE);
                fbLogin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
    //log out
                        LoginManager.getInstance().logOut();
                        gotoLogin();
                    }
                });
            }
    

    U can logout by using LoginManager but u have to use graph request also. I am talking about log out completely so, that next time u can login with different account.

    new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
                .Callback() {
            @Override
            public void onCompleted(GraphResponse graphResponse) {
    
                SharedPreferences pref = DashBoard.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                editor.clear();
                editor.commit();
                LoginManager.getInstance().logOut();
    
                Intent logoutint = new Intent(DashBoard.this,MainActivity.class);
                logoutint.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                   startActivity(logoutint);
    
            }
        }).executeAsync();
    

    By the help of shared preferences here u can logout completely. And next time u can logout with different account.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.