erdomester erdomester -5 years ago 226
Android Question

Facebook authentication without login button

I have followed some Facebook API 3.0 tutorials, including the Login/Logout and the Publish To Feed examples. So the login works this way:

  1. App opens, shows a fragment which displays a login button

  2. User clicks login, the authentication is done via the referenced FacebookSDK library (com.facebook.widget.LoginActivity) and the provided code using sessions.

  3. User is redirected to next screen

I don't want to make the user to login this way. I want them to use my app without any login/registration, then if they click on a facebook specific feature e.g. share a note on Facebook, then the app should ask them whether they let Facebook use their app or something, you know the usual stuff. Without this I get a nullpointer in the publishFeedDialog() function as session is null, because no login has been made.

So my question is, how can I ignore the SplashFragment with the Login button, so when the user clicks on a Facebook feature in my app, no new screen is displayed with a login button, but only the default Facebook authentication window that users are used to?

Answer Source

@erdomester, @sromku

Facebook launch new sdk version 4.x where Session is deprecated,

There new concept of login as from facebook

LoginManager and AccessToken - These new classes perform Facebook Login

So, Now you can access Facebook authentication without login button as



private CallbackManager mCallbackManager;

public void onCreate(Bundle savedInstanceState) {


    mCallbackManager = CallbackManager.Factory.create();

            new FacebookCallback<LoginResult>() {
                public void onSuccess(LoginResult loginResult) {
                    Log.d("Success", "Login");


                public void onCancel() {
                    Toast.makeText(MainActivity.this, "Login Cancel", Toast.LENGTH_LONG).show();

                public void onError(FacebookException exception) {
                    Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();


    Button btn_fb_login = (Button)findViewById(;

    btn_fb_login.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
              LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));



If you don't add the following, it won't work (rightly pointed out by @Daniel Zolnai in comment below):

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download