Wai Yan Hein Wai Yan Hein - 3 months ago 18
Android Question

Callback functions are not called in Android Facebook Login

I am developing an Android app. In my app, I am integrating Facebook Login. I have done developing Facebook Login once before. When I develop this time, Facebook callback functions are not called. I cannot check the error as well. I have no idea with what is wrong. Please see what I did below.

I installed Facebook SDK using grandle

compile 'com.facebook.android:facebook-android-sdk:4.1.0'


Then I generated key hash and set in the Facebook developer settings as follow
enter image description here

This is my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tonightfootballreport.com.tfr" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:name="tonightfootballreport.model.TfrApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<activity
android:configChanges="orientation|screenSize"
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:configChanges="orientation|screenSize"
android:name=".FacebookConfigActivity"
android:label="Facebook"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="tonightfootballreport.com.tfr.FacebookConfigActivity"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />

<provider android:authorities="com.facebook.app.FacebookContentProvider1817380365215100"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />
</application>

</manifest>


As you can see Facebook Login is not in main activity and so I set FacebookConfigActivity in Developer Settings.

Then when I click a button in MainActivity, FacebookConfigActivity will be opened and perform Facebook Login. Below is the code.

public class FacebookConfigActivity extends AppCompatActivity {
private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext(), AppConfig.FACEBOOK_REQUEST_CODE);
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_facebook);
setUpCallBack();
loginInToFacebook();
}


private void loginInToFacebook()
{
LoginManager.getInstance().logInWithReadPermissions(FacebookConfigActivity.this,Arrays.asList("public_profile"));
}

private void setUpCallBack()
{
callbackManager = CallbackManager.Factory.create();

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
//show share alert
//then share
Log.i("LOGIN_SUCCESS","LOGIN_SUCCESS");
}


@Override
public void onCancel() {
Toast.makeText(getBaseContext(), "Operation canceled", Toast.LENGTH_SHORT).show();
finish();
}

@Override
public void onError(FacebookException error) {
Toast.makeText(getBaseContext(), "Error in connecting to Facebook", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}


As you can see, it is just simple Facebook Login. So when I click login button, I enter credentials to the Facebook Login form you can see below.
enter image description here

Then after I click OK in the next step, FacebookConfigActivity is displayed and no callback function is called. If I click cancel, Facebook Cancel callback is not called as well. So please what is the possible error? How can I check it? Why my code is not working? Please help me.

Answer

You forgot to put listener in onActivityResult()

put below code in onActivityResult method of activity:

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