I am trying to make a class that handles Facebook logins in my application. My problem is it doesn't work on all activity. On that activity it doesn't call the callback. After the login and authorize application webview is dismissed, the callback doesn't fire. The last state that printed in Logcat is OPENING
Here is my code:
public void doLogin() { if ((Session.getActiveSession() == null || !Session.getActiveSession().isOpened())) { List<String> permissions = new ArrayList<String>(); permissions.add("email"); // start Facebook Login openActiveSession(activity, true, new Session.StatusCallback() { // callback when session changes state @Override public void call(Session session, SessionState state, Exception exception) { Log.d("Sessionstate", state.toString()); if (session.isOpened()) { // make request to the /me API Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { @Override public void onCompleted(GraphUser user, Response response) { if (prgCheckFB.isShowing()) prgCheckFB.dismiss(); if (user != null) { Log.e("facebookid", id); doSomething(user); } } }); } }, permissions); } } private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback callback, List<String> permissions) { Session.OpenRequest openRequest = new Session.OpenRequest(activity) .setPermissions(permissions).setCallback(callback); Session session = new Session.Builder(activity).build(); if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) { Session.setActiveSession(session); session.openForRead(openRequest); return session; } return null; }
doSomething
is a function that will save the user data in a shared preference.
Is there anything wrong? The function works in some activity but not ALL activities.
Testing Your CallbackGo to your Facebook profile's Apps and Websites settings tab: https://www.facebook.com/settings?tab=applications. Click the View Removed Apps and Websites link. In the popup, click the View button to the right of the application. In the window, appeared click Send Request to trigger your callback.
The CallbackManager manages the callbacks into the FacebookSdk from an Activity's or Fragment's onActivityResult() method. Nested Class Summary. Modifier and Type. Class.
Configure Your Facebook App for Android First, log into Facebook developer account and do the following: Go to the App Dashboard; Click My Apps and create a new app; Go to Settings > Basic to see the App Details Panel with your App ID, App Secret as well as other app details.
maybe you forget to handle the results after login...
check the override method onActivityResult..
because it handle the results back to the MainActivity,
maybe this can help your problem..
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); }
Update 1
For newer SDK use:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); }
Having the attribute android:noHistory="true"
set for your callback activity in the AndroidManifest.xml file will also result with the callback methods not firing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With