Thursday, 28 February 2013

Facebook Android SDK 3.0 - how to log out and close a session

In the previous two Blog posts (here and here), I showed how to connect a user to their Facebook account (i.e. open a Facebook session) and how to make a Facebook API request (requesting additional Facebook permissions en route if necessary). In this final Blog post, I'll show to close the Facebook session, as follows:

public void disconnectFacebookAccount()
{
  showProgressDialog("Disconnecting Facebook account...");

  new AsyncTask<Void, Void, Boolean>()
  {
    @Override
    protected Boolean doInBackground(Void... params)
    {
      if (Session.getActiveSession() != null)
        Session.getActiveSession().closeAndClearTokenInformation();

      clearFacebookInfoFromSharedPreferences();

      // perform any other operations you need to do perform here
      // such as clearing local database tables and so forth

      return true;
    }

    @Override
    protected void onPostExecute(Boolean result)
    {
      // safety check
      if (isFinishing())
        return;

      if (result == null
          || result == false)
        onFailedFacebookDisconnect();
      else
        onSucceededFacebookDisconnect();
    }
  }.execute();
}

That's it! That's the end of my Facebook Android 3.0 series. You should have enough know-how now for all kinds of Facebook API requests! :)

5 comments:

Unknown said...

Can you send me a project temna_nich@mail.ru or share it in github.

adil said...

Hi Galina,

Apologies but I don't have it as a project. Just put the tutorials together to explain/demonstrate the core concepts of session management. Feel free to copy the code and use it as required :)

Regards,
Adil

Kairat said...

And, what do these methods look like?
clearFacebookInfoFromSharedPreferences(); onFailedFacebookDisconnect();
onSucceededFacebookDisconnect();

adil said...

Kairat, those are methods outside the scope of this tutorial and for you to define as you wish when the disconnect request succeeds, fails etc. You could simply show a Toast alerting the user as to what happened.

Kairat said...

Thank you for youe response Adil. I am integratin FacebookSDK in my app. So when the app starts and user is logged in, then it shows a "proceed as someUser" Dialog. So if I press "YES" button on Dilaog it continues. But if I press "Change", then I want it to completely logout from Facebook and open a FacebookSDK LoginActivity so I can enter new login and password. Can you explain me how to implement that please? Yhank you!