0
votes

I want to integrate the Facebook login dialog into the activity. If a custom button is pressed, the app must login via Facebook (or autologin if the user and password were entered before). I managed to do this by adding the LoginButton in the activity, but I want to override it's functionality. If the user clicks the button, the action behind LoginButton will be executed.

I need this because I want to verify first if the device has an active internet connection, but with my implementation the app tries to login with Facebook and after that the alert dialog with 'no internet connection' appears.

Here's my activity. Facebook login starts when I click the authButton, even no action is set in the onClickListener.

public class LoginActivity {

private Activity mActivity;
private User userR;

private static final String TAG = "Login";
private UiLifecycleHelper uiHelper;

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

    mActivity = this;

    LoginButton authButton = (LoginButton) findViewById(R.id.authButton1);
    authButton.setReadPermissions(Arrays.asList("public_profile", "email"));
    authButton.setOnClickListener(onClickListener);
    authButton.setText("Connect with Facebook");
    authButton.setBackgroundResource(R.drawable.button_facebook);
    authButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);

    uiHelper = new UiLifecycleHelper(mActivity, callback);
    uiHelper.onCreate(savedInstanceState);

}

private OnClickListener onClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.authButton1:
            break;
        }
    }
};

@Override
protected void onStart() {
    super.onStart();

}

@Override
public void onBackPressed() {

}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

@Override
public void onResume() {
    super.onResume();
    Session session = Session.getActiveSession();
    if (session != null && (session.isOpened() || session.isClosed())) {
        onSessionStateChange(session, session.getState(), null);
    }
    uiHelper.onResume();

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(mActivity, requestCode, resultCode, data);
    userR = new User();

    if (Session.getActiveSession().isOpened()) {
        // Request user data and show the results
        Request.newMeRequest(Session.getActiveSession(), new GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {
                if (null != user) {
                    //getting email, name, gender
                }
                login();

            }
        }).executeAsync();

    }
}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    Session.getActiveSession().closeAndClearTokenInformation();
    uiHelper.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

public void login() {
    //login to server with data fetched from Graph user
}

Here is the XML for the LoginButton com.facebook.widget.LoginButton

        android:id="@+id/authButton1"
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="30dp"
        fb:login_text="Connect with Facebook"
        fb:logout_text="Connect with Facebook"
1

1 Answers

0
votes

The setOnClickListener on the LoginButton is overridden so you don't accidentally replace it, and instead proxies to the listener you set after login handling is done.

You should consider disabling or hiding the button altogether if there's no internet connection, rather than waiting until the user clicks on it.