Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a logout callback for facebook sdk in android

I have integrated Facebook sdk in my android app. As described in the manual I added the login callback for facebook. But I have to change the UI if the user logs out from facebook. Where do I put that code. My code for login is

         /**
         * Login Callback for facebook login
         */
        callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {

                @Override
                public void onSuccess(LoginResult loginResult) {
                    //Call updateUI()

                    setData("provider","facebook");
                    loginType = LoginTypes.FB_LOGIN;
                    isLoggedin = true;
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(
                                        JSONObject object,
                                        GraphResponse response) {
                                    // Application code

                                    txtName.setText(response.toString());
                                    updateUI();
                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,email");
                    request.setParameters(parameters);
                    request.executeAsync();
                }

                @Override
                public void onCancel() {
                    editText_message.setText("Login Cancelled.");
                    // App code
                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                }
            });
like image 790
bytestorm Avatar asked May 14 '15 09:05

bytestorm


People also ask

What SDK does Facebook use?

To learn more about using Facebook development tools, see App Development. The current version of the Facebook SDK for Android is version 14.1. 1 and requires the Android API 15. Code and samples for the Facebook SDK for Android are available on GitHub.


1 Answers

there are 2 possible ways:

1) you need to overwrite in on create AccessTokenTracker like this:

accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken,
                                                       AccessToken currentAccessToken) {
                    if (currentAccessToken == null) {
                        //write your code here what to do when user logout
                    } 
                }
            }

2) You can call LoginManager.logOut() to log out the user

hope this will help you :)

like image 146
Stan Malcolm Avatar answered Sep 20 '22 13:09

Stan Malcolm