Session Management

This is the fifth part of a 6 posts series on Fragment oriented application architecture. In the previous post I talked about efficiently handling back button press inside fragment. In this part I am going to talk about session management in Fragment oriented application, by explaining integration of Facebook SDK.

(Sample application’s source code and README)

In a fragment oriented application, we can conveniently manage all session related code in the activity and all its fragments would utilise it. Facebook SDK is quite in sync with this approach. Implementation of Facebook session is closely bound to an activity. And then this session is accessible throughout the application. As has been discussed before, if an application requires to sign in from different portions of it, it will be way more convenient to have those portions as parts of the same activity. So that the authentication code need not be duplicated.

Sample app has been integrated with Facebook SDK 3.15.

In sample app, FacebookInterface has a method called onClickLogin(). BaseFragment keeps a reference of this interface. As every other fragment is directly or otherwise a child of BaseFragment, this method is at disposal of them all. So, if an open Facebook Session is not available, this onClickLogin() method of Home can be called from any of the fragments to kick off authentication flow before trying to post something on wall.

In sample app, GreetingsFragment and ThirdStepFragment require to post a status on wall. From both these places, Facebook session is check for open state before posting status.

// POSTING STATUS FROM FRAGMENTS

Session facebookSession = Session.getActiveSession();
if(facebookSession.isOpened()) {
    // Check FacebookUtils class in Sample Application's code.
    FacebookUtils.postOnWall(getActivity(), "Title", "Message", "link");
} else {
    // Kick off authentication if there is no open Session
    facebookInterface.onClickLogin();
}

In the next post I’m going to talk about Retaining Fragment view-state.

Tagged as:

One thought on “Session Management

Leave a Reply

Your email address will not be published. Required fields are marked *