Java – Android Facebook login callback does not work properly

I tried to use Facebook login to access user profile. But I'm confused about Facebook tutorial. So far, I think I've successfully logged in, but I can't get my 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 one rror(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);
}
}

resolvent:

In your manifest file, an activity that contains a Facebook login button may have a nohistory attribute as follows:

 <activity
  ...
  android:noHistory="true"
  ...  >

Deleting it will fix your problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>