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.
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:
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:
(well done, Facebook team – singleton at last!) and, depending on what button style you pick:
for native Facebook style and then:
starting a log in an event automatically on user’s click or:
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.
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.