FB Android: Session: an attempt was made to open an already opened session

I'm implementing Facebook Login with android-simple-facebook library. It works well in Eclipse example, but in my JNI application it crashes with the following error:

java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
at com.facebook.Session.open(Session.java:985)                                                   
at com.facebook.Session.openForPublish(Session.java:423)                                         
at com.sromku.simple.fb.SessionManager.openSession(SessionManager.java:237)                      
at com.sromku.simple.fb.SessionManager.reopenSession(SessionManager.java:332)                    
at com.sromku.simple.fb.SessionManager.isLogin(SessionManager.java:123)                          
at com.sromku.simple.fb.SessionManager.login(SessionManager.java:57)                             
at com.sromku.simple.fb.SimpleFacebook.login(SimpleFacebook.java:171)                            
at com.js.helpers.FBHelper$2.run(FBHelper.java:109)                                              
at android.os.Handler.handleCallback(Handler.java:730)                                           
at android.os.Handler.dispatchMessage(Handler.java:92)                                           
at android.os.Looper.loop(Looper.java:137)                                                       
at android.app.ActivityThread.main(ActivityThread.java:5103)                                     
at java.lang.reflect.Method.invokeNative(Native Method)                                          
at java.lang.reflect.Method.invoke(Method.java:525)                                              
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)               
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)                                  
at dalvik.system.NativeStart.main(Native Method)                                                 

My login implementation is pretty straitforward:

    public static void login() {

    final OnLoginListener onLoginListener = new OnLoginListener() {
        @Override
        public void onFail(String reason) {
            Log.w(TAG, "onLoginListener failed: " + reason);
        }

        @Override
        public void onException(Throwable throwable) {
            Log.e(TAG, "OnLoginListener exception: ", throwable);
        }

        @Override
        public void onThinking() {
            // show progress bar or something to the user while login is happening
            Log.i(TAG, "onLoginListener in progress...");
        }

        @Override
        public void onLogin() {
            // change the state of the button or do whatever you want
            Log.i(TAG, "Logged in");
            loginCB();
        }

        @Override
        public void onNotAcceptingPermissions(Permission.Type type) {
            Log.i(TAG, "onLoginListener NotAcceptingPermissions: " + type);
        }
    };

    mSimpleFacebook.login(onLoginListener);
    Log.i(TAG, "FBHelper.login");
}

loginCB is my native callback function.

The most annoying thing that the problem is floating, the application may login sometimes, but mostly crashes. I'll appreciate any help!

Answers


It looks like you missed: onActivityResult in your Activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data); 
    super.onActivityResult(requestCode, resultCode, data);
} 

Let me know if it worked for you.


I'm sure it was a JNI problem. The following combination made my code working:

  • Moving OnLoginListener one level up
  • Adding runOnUiThread
  • Adding try/catch block
  • Checking mSimpleFacebook != null and fixing IsLoggedIn function in order not to call for Login if already logged in (well, I see that this situation is handled in library, but Facebook really tried to make a duplicate login).

So, my final code is:

private static OnLoginListener mOnLoginListener = new OnLoginListener() {
         // the same code as in question above
};

public static void login(final Activity activity) {

    activity.runOnUiThread(new Runnable() {
        public void run() {
            if (mSimpleFacebook == null) 
                mSimpleFacebook = SimpleFacebook.getInstance(activity);
            try {
                mSimpleFacebook.login(mOnLoginListener);
            } catch (Exception ex) {
                Log.e(TAG, "Login error: ", ex);
            }
        }
    });
}

Need Your Help

Custom queries with Breeze JS (Fiql + Breeze)

breeze

Im new to breeze js, I understand that breeze has it's own query language related to OData

Migrating Eclipse Project to Android Studio with dependencies

java android import dependencies android-gradle

Imported an Eclipse Project to Android Studio having library dependencies of Sliding Menu, ViewPager, PullToRefresh. Import was successful but after syncing grade files getting duplication errors in