简体   繁体   中英

Facebook android sdk 3.0 login issue

I have recently upgraded my code to use FB android SDK 3.0. My application has four activities and DOES NOT use fragments. Strangely my code works without any problem on android 2.3.4. But in android 4.0.4, the state of session is always 'OPENING'. I'm using UiLifeCycleHelper and below is the the code in launcher activity.

SharedPreferences mPrefs;
String access_token = mPrefs.getString("access_token", null);

Session currentSession = Session.getActiveSession();
if(currentSession == null || currentSession.getState().isClosed())
{
    if(access_token!=null)
    {
        SharedPreferences.Editor editor = mPrefs.edit(); 

    //One time upgrade from FB SDK 2.0 to 3.0
        editor.putString("access_token", null);
        editor.commit();

        AccessToken new_access_token = AccessToken.createFromExistingAccessToken(access_token, null, null, null, null);
        currentSession.open(new_access_token, myCallback);
        Session.setActiveSession(currentSession);
    }
    else
    {
        Session.openActiveSession(this, true, myCallback);
    }
}
else if (!currentSession.isOpened())
{
     OpenRequest openRequest = new OpenRequest(this).setCallback(myCallback);
     openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
     openRequest.setPermissions(Arrays.asList(permissions));
     openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
     currentSession.openForRead(openRequest);

}
else if(currentSession.isOpened())
{
    handleRequest(currentSession);

}


StatusCallback myCallback = new StatusCallback()
{

    @Override
    public void call(Session session, SessionState state, Exception exception)
    {
        handleRequest(session);

    }
};


public void handleRequest(Session activeSession)
{

    Request new_request = new Request(activeSession, "/me/friends", null, null, new Callback()
    {

        @Override
        public void onCompleted(Response response)
        {

            //Process the response and make UI changes.
        }

});
}

First I suspected that the problem was due to an old version Facebook app (1.9). So I upgraded to latest version(3.2). Still the problem persists on android 4.0.4.

Here is my code in onActivityResult callback

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        uihelper.onActivityResult(requestCode, resultCode, data);
}

Is there anything wrong?

Did you remember to add the onActivityResult code?

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}

Also give a look at this other question

I solved it myself. Just commented out handleRequest in myCallback.

StatusCallback myCallback = new StatusCallback()
{

    @Override
    public void call(Session session, SessionState state, Exception exception)
    {
    //handleRequest(session);

    }
};

This time my app work without much problems on both android 2.3.4 and 4.0.4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM