Facebook Login Api Android (Unexpected call to LoginManager.onActivityResult)

Viewed 281

I am having an issue with Facebook Login Api: specifically I am getting: Unexpected call to LoginManager.onActivityResult in onError callback:

Here is the code:

class MainActivity : AppCompatActivity() {

    lateinit var callbackManager: CallbackManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        callbackManager = CallbackManager.Factory.create()
        val loginButton = findViewById<LoginButton>(R.id.login_button)

        loginButton.setPermissions("email")
        loginButton.registerCallback(callbackManager, object : FacebookCallback<LoginResult?> {
            override fun onSuccess(loginResult: LoginResult?) {
                Log.d("FACEBOOKLOGIN", "onSuccess: ")
            }

            override fun onCancel() {
                Log.d("FACEBOOKLOGIN", "cancel: ")
            }

            override fun onError(exception: FacebookException) {
                Log.d("FACEBOOKLOGIN", "exception: ")
                //error occurs here
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        callbackManager.onActivityResult(requestCode, resultCode, data)
    }
}

Manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ramones.loginapplication">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.LoginApplication">
        <activity android:name=".MainActivity"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>

        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
    </application>

</manifest>

P.S. This is a test app. Just checking out fb api and messing around. When I am logged out and proceed to log in via the app. Everything is fine. I am asked to grant permissions and proceed with log in. Fb button changes state.

The issue occurs if test user or myself is already logged in. A somewhat different dialog appears, even though it asks for same permissions" and suddenly just closes ending in onError with: Unexpected Call to LoginManager.OnActivityResult".

Any help is appreciated.

Thanks in advance.

0 Answers
Related