Search code examples
androidfacebook-android-sdk

How can i find my current login status - facebook API android


I am using Facebook Login Button to integrate Facebook Login to my app.

  1. When i press Facebook Login Button , it authenticates my Credentials and if successfully done , then change text to Log Out & Go inside my app.

  2. Now when i use app and i go out of App and when i again come back to Login Screen. Here i see logout - As i authenticate myself before and i didn't logout.

  3. So My question is how to differentiate when i am already logged in or my session is expired ?

Here is a code i am using:

LoginButton authButton;

authButton = (LoginButton) findViewById(R.id.authButton);
// set permission list, Don't forget to add email

authButton.setReadPermissions(Arrays.asList("basic_info", "email"));

authButton.setSessionStatusCallback(new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state,
                Exception exception) {

            authButton.setReadPermissions(Arrays.asList("basic_info",
                    "email"));
            System.out.println("Inside Call");
            if (session.isOpened()) {
                fbToken = session.getAccessToken();


                                }

                            }
                        });

.....................................

Thank you very much in advance.

Regards, Rakesh


Solution

  • You can get the session using Session.getActiveSession() and then listen for session change event

    Session session = Session.getActiveSession();
    if (session != null && (session.isOpened() || session.isClosed()) ) {
        onSessionStateChange(session, session.getState(), null);
    }
    

    And then check the session status:

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