This sample project will show how to use Facebook Android SDK to login and logout from the social network using a custom button and saving the data you might need (in this project we only save user full name and mail address) using the library we created to make an easier implementation of Facebook Android SDK.
- Download Facebook Android SDK from Developer site and uncompress it
- Download the library folder and add it as a Module of your app
- Once the module has been added to your project, Facebook SDK will most probably lack a few dependencies, that are contained in its library subfolder: to overcome this issue, simply open facebook/build.gradle file in your project and paste inside the content of the one you can find in this repository in the same location, that will actually add those dependencies to the SDK.
Note: if you experience issues with minSdkVersion or targetSdkVersion, simply change them according to your app/build.gradle file.
This library comes with a FacebookLoginFragment and a FacebookLoginActivity ready to be extended: by doing this, you will be asked to implement the following methods:
onSessionOpen(): this method is called every time a Session Opening is completed (for instance, this method will be called on successfull login), including during theonResume()method of yourFragmentorActivityif your app is called from links.onSessionClose(): this method is called every time a Session Closing is completed (for instance, this method will be called on successfull logout), including during theonResume()method of yourFragmentorActivityif your app is called from links.onSessionEvent(): this method is called every time a Session Change is completed (but the Session is not yet opened nor closed), including during theonResume()method of yourFragmentorActivityif your app is called from links.onSessionNullResume(): this method is called during theonResume()phase of yourFragmentorActivityif your app is called from links and the Facebook Session is null. (Note: one case in which this method can be invoked is when your app is launched from a link but the user did not logged in for the first time yet)
Once you successfully extended FacebookLoginFragment or FacebookLoginActivity, you will have the possibility to invoke the login and logout methods
public Session login(String... profiles) throws FacebookHelperExceptionpublic void logout() throws FacebookHelperException
Note: both methods will throw a FacebookHelperException in case the Fragmentis not attached or it has not been created.
Refer to the sample project in order to have a clear look on how to use this library or download the APK from this repository.
This library is provided as is under the terms of MIT License
- StackOverflow user Rikin Prajapati for hints about the logout method
- StackOverflow User Cristiana214 for hints about the Gradle issue