Search code examples
androidfacebookfacebook-android-sdk

Dismiss facebook login dialog android


I've integrated facebook sdk v-4 with my android app. Here I've worked facebook login in 2 activities.

1) In one activity fb login is done via LoginButton. Where after successful login the fb login dialog is dismissed by facebook sdk automatically.

code:

fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        ProfileTracker profileTracker;

        @Override
        public void onSuccess(LoginResult loginResult) {
            Logger.d("FBLOGIN", "login successful. User ID: "
                    + loginResult.getAccessToken().getUserId()
                    + "\n" +
                    "Auth Token: "
                    + loginResult.getAccessToken().getToken());

            com.facebook.Profile fbProfile = com.facebook.Profile.getCurrentProfile();

            if (fbProfile == null) {
                profileTracker = new ProfileTracker() {
                    @Override
                    protected void onCurrentProfileChanged(com.facebook.Profile oldProfile, com.facebook.Profile currentProfile) {
                        profileTracker.stopTracking();
                        doTaskWithFbProfile(currentProfile);
                    }
                };
                profileTracker.startTracking();
            } else {
                doTaskWithFbProfile(fbProfile);
            }

        }

        @Override
        public void onCancel() {
            Logger.d(TAG, "fb Login attempt canceled.");
        }

        @Override
        public void onError(FacebookException e) {
            Logger.e(TAG, "fb Login attempt failed." + e);
            Toast.makeText(LoginActivity.this,R.string.str_please_check_your_internet_connection,Toast.LENGTH_LONG).show();
        }
    });

 private void doTaskWithFbProfile(com.facebook.Profile fbProfile) {
    Logger.d("FBLOGIN", "id - " + fbProfile.getId());
    Logger.d("FBLOGIN", "name - " + fbProfile.getName());
    Logger.d("FBLOGIN", "pic - " + fbProfile.getProfilePictureUri(150, 150));

    com.android.circlecare.models.common.Profile profile = new com.android.circlecare.models.common.Profile();
    profile.setFBID(fbProfile.getId());
    profile.setName(fbProfile.getName());
    profile.setProfilePhoto(fbProfile.getProfilePictureUri(150, 150).toString());

    Intent intent = new Intent(this, LoginWithFacebookActivity.class);
    intent.putExtra(LoginWithFacebookActivity.EXTRA_PROFILE, profile);

    startActivity(intent);
    finish();

}

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

2)On the other hand I used LoginManager to log in to fb. Where after successful login the login dialog does not get dismissed, instead it stays in front and again after login the dialog doesn't go.

code:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

 FacebookSdk.sdkInitialize(this.getApplicationContext());

 mFacebookCallback = new FacebookCallback<LoginResult>() {
        ProfileTracker profileTracker;

        @Override
        public void onSuccess(LoginResult loginResult) {
            Profile fbProfile = Profile.getCurrentProfile();

            if (fbProfile == null) {
                profileTracker = new ProfileTracker() {
                    @Override
                    protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                        profileTracker.stopTracking();
                        getAllFriends();
                    }
                };
                profileTracker.startTracking();
            } else {
                getAllFriends();
            }
        }

        @Override
        public void onCancel() {
            Logger.d(TAG, "fb Login attempt canceled.");
        }

        @Override
        public void onError(FacebookException error) {
            Logger.d(TAG, "Error : " + error.getMessage());
            finish();
        }
    };

    callbackManager = CallbackManager.Factory.create();

    if (hasAccess)
        getAllFriends();
    else
        loginWorks();
}


  private void loginWorks() {
    loginManager = LoginManager.getInstance();
    loginManager.registerCallback(callbackManager, mFacebookCallback);

    ArrayList<String> publishPermission = new ArrayList<>();
    publishPermission.add("publish_pages");
    publishPermission.add("publish_actions");
    loginManager.logInWithPublishPermissions(this, publishPermission);

    ArrayList<String> readPermission = new ArrayList<>();
    readPermission.add("user_friends");
    readPermission.add("public_profile");
    readPermission.add("user_about_me");
    loginManager.logInWithReadPermissions(this, readPermission);
}

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

I've used almost same codes in both the cases, but I wonder why login dialog isn't get dismissed when working with LoginManager.

Can any one point out the problem or is there any api to dismiss fb login dialog(web view).


Solution

  • In your loginWorks() method, you're literally calling login twice, which is probably actually showing 2 different login screens, and why it looks like the dialog is not dismissing after you finish one of them. Try removing the loginWithPublishPermissions call, and put it in the callback from the read permissions call if you really need it.