Facebook Integration from Scratch

in Backend by Maja on October 28th, 2015

Have you ever felt like you are struggling over and over again with Facebook login implementation on Android? Or maybe you’re just asking yourself the same question as I do - why on earth Mark makes you manage token sessions by yourself? Save it in on SaveInstanceState and restore every time your app resumes? (Remember even older UiLifecycleHelper? Very bad times for developers, btw). Each and every one of us experienced doubled or tripled callbacks from time to time, with various token states causing multiple MeRequests firing off. Such a waste of data transfer. The fight is over, folks. Or it appears to be.

Online repository

The first new Facebook SDK feature is an online repo available in Maven Central, which can only mean one thing - Gradle integration. No more downloading jars from developers.facebook.com, no more including them in libs folder, no more adding local dependecies in build.gradle file. A clean, acceptable, one-line solution:

compile 'com.facebook.android:facebook-android-sdk:4.1.0'

New SDK’s version available? Android Studio will tell you, so just make a change in version code, synchronize gradle scripts and consider it done.

 

Login implementation - cosmetic changes or a real breakthrough?

Forget LoginActivity included in Android Manifest, now it is called FacebookActivity. The new one extends FragmentActivity instead of Activity, which ensures better backward compatibility as a part of Support Package. I’m pretty sure it takes a lot of time and effort to make your app available on older devices, but it’s not only a matter of efficiency. It’s about consistency. Material design may look clean and elegant on Lollipop, but right now we all make use of AppCompat and Support libs along the whole project, including Facebook integrated parts.

The best news is Session class has also been removed in favor of AccessToken, LoginManager and CallbackManager classes. In order to track tokens and profile updates, you can use simple Trackers with callbacks fired by every token or profile change. What is more, access token is automatically cached as soon as SDK sets it. In other words, we won’t be obliged to take care of sessions and tokens by hand ever again.

Developing a login feature is a real pleasure now, especially in comparison to the amount of code (and hours) required to achieve the exact same result in SDK 3.x. It takes only a few lines:

FacebookSdk.sdkInitialize()

(well done, Facebook team - singleton at last!) and, depending on what button style you pick:

loginButton.set[Read/Publish]Permissions(...)

for native Facebook style and then:

loginButton.registerCallback()

starting a log in an event automatically on user’s click or:

LoginManager.getInstance().logInWith[Read/Publish]Permissions(...)

in manually set OnClickListener for a customized button.

 

Whichever way you go, you stil need a CallbackManager to manage Facebook event results, so create it once and remember about passing arguments to it in your onActivityResult method. Just once.

And more

According to Facebook developers, they improved error management (seriously, it’s high time) and sharing options. We’ll see about that in our later projects, but it looks like it may actually work.

We all know where to find working examples (thank the Universe for SO), instead of dealing with a brief official specification, but if you have any experience of your own with the new Facebook SDK for Android integration, feel free to share it with us.


← back to the blog

You May Also Like

How to Find the Best Technical Partner for Your Startup

by Damian in on November 14th, 2015

Finding Tech People to your startup is not an easy game. It takes time, energy and money. So how to build a strong Tech Team without losing your patience and motivation? Let’s find out.

Why I Won’t Sign Your NDA

by Damian in on December 29th, 2016

"I have a great unique idea, but before I share it we need to sign and NDA" - this is what I get a couple of times in a week. And here is the explanation, why I don't do it.

Why Should Clients Spend Some Time with the Software Provider?

by Damian in , on January 5th, 2017

Crafting a good piece of software requires patience and trust. It’s an intensive process that brings people together. Before the start of each IT project, it’s worth spending some time with the client and knowing each other.

Interested in Creating a Successful Project?

Contact us and together we'll bring your ideas to life!