Android Facebook Login callback not working

im trying to get access to user profile using facebook login. but im so confused with facebook tutorial. so far i think i’ve succeeded with logging in but was unable to get Profile. here’s my code.

my fragment class:

public class FBLoginFragment extends Fragment {


private LoginButton loginButton;
private CallbackManager callbackManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_fblogin, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    loginButton = (LoginButton) view.findViewById(R.id.login_button);
    loginButton.setFragment(this);
    loginButton.setReadPermissions("user_friends");


    // Callback registration
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();

            // App code
            if(profile != null) System.out.println("login successful " + profile.getName());
        }

        @Override
        public void onCancel() {
            // App code
            System.out.println("login cancel");
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
            System.out.println("login fail");
        }
    });


}

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

    callbackManager.onActivityResult(requestCode, resultCode, data);
}
}

my main activity code:

public class LoginActivity extends ActionBarActivity {

private Fragment fragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
}
}


Source: java

Leave a Reply