0

I am using fb SDK v2.2 for android and I followed the tutorial so I am not going to repeat the code here. I am using the loginbutton of Android sdk and I set the uiHelper to have a callback on session start like this

   uiHelper = new UiLifecycleHelper(this, new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    });

when the user logs in by clicking on the fb login button then the onSessionStateChange is called (expected) and I call a certain code initialize user info.

Now if the user kills the app. Then opens the app again, it starts with the fact that the user logged in( Great). The problem happens that the onSessionStateChange is called again and then I end up "doing another heavy work" based on the fact that the session state is opened (as if the user is logging in again). I don't want this behavior. I just want to do this heavy work when the user clicks on log in and actually go through the login scenario not every time he opens the app and he is already logged in.

Any idea how to achieve that? Basically not to get the sessionStateChange called OR somehow differentiate between the case when he clicks on login and when he is actually logged in. Iam sure the sdk has it but I can't seem to find it

Thank you

Snake
  • 14,228
  • 27
  • 117
  • 250

1 Answers1

0

The StatusCallback.call() method is called every time the state of the session changes. When you cold-start an app, and create a Session object, it will go through several state changes, and every one of those will trigger a StatusCallback.call().

You can:

  1. set an OnClickListener on your LoginButton, and listen to click events (possibly setting some state telling your app that future session changes are triggered by a button click), or
  2. add a UserInfoChangedCallback to the LoginButton, and put your heavy lifting in that callback (note that you'll also need to set the fetch_user_info attribute to be true for the LoginButton), see https://developers.facebook.com/docs/reference/android/current/interface/LoginButton.UserInfoChangedCallback/
  3. when you construct a Session, see if it's in the CREATED_TOKEN_LOADED state, in which case it will proceed to open without the user having to click on anything, see https://developers.facebook.com/docs/reference/android/current/class/SessionState/
Ming Li
  • 15,672
  • 3
  • 37
  • 35
  • Thank you for the solution. If I do the second the 2nd case, is login life cycle of the button click initiated normally or do I have to manually call the login api (like onclick). And for the 3rd case, can you ellaborate a bit more? What do you mean by construct a session? I usually get the session from the call back method... Thanks again – Snake Dec 09 '14 at 02:42
  • In the 2nd case, you don't need to manually call anything. For 3, if you're not creating a Session explicitly (i.e. you're only using the LoginButton to get the session), then it doesn't really apply. – Ming Li Dec 09 '14 at 19:54
  • Thank you.. I will give it a try and let you know if it does not work :) – Snake Dec 10 '14 at 06:11