简体   繁体   中英

Check if the user when log out from facebook in android facebook sdk 4

my problem is that I hav an app which can connects to facebook using facebook sdk new version(4.0) and I want that when the user logs into facebook , a textview appears and show it's name and when log out clicked the textview disappears .

the first part is done and I can get the data I want:

    btnLoginButton.registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        fBLogInClicked = true;
                        GraphRequest.newMeRequest(loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                        public void onCompleted(JSONObject object,
                                            GraphResponse response) {
                                        try {

                                    String jsonresult = String
                                                        .valueOf(object);
                                    String str_firstname = object    .getString("first_name");
                                                String str_lastname = object
                                                        .getString("last_name");
                                                fullName = str_firstname + " "
                                                        + str_lastname;

                                            } catch (JSONException e) {

                                                e.printStackTrace();
                                            }
                                            if (fullName != null) {
                                                txtFBEmail
                                                        .setVisibility(View.VISIBLE);
                                                txtFBEmail.setText(fullName);
                                            }

                                        }
                                    }

                                }).executeAsync();
                    }

but after logout I don't know how to understand that logout is clicked and then to set visibility of txtFBEmail gone.

I tryed this code on onCreate() but just when I restart the app is partly useful and when log out clicks I can not give the order to disappear the textview.:

AccessToken fb_token = AccessToken.getCurrentAccessToken();
     if(fb_token == null) {
         txtFBEmail.setVisibility(View.GONE);
     }

this method would help you to do what ever you want the exact time that logout is clicked:

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

Use this code for facebook sdk 4.

 profile = Profile.getCurrentProfile().getCurrentProfile(); if (profile != null) { // user has logged in txtFBEmail.setVisibility(View.visible); } else { // user has not logged in txtFBEmail.setVisibility(View.GONE); } 

for more information about new facebook-sdk 4 integration you can check my answer here. Facebook Login with SDK4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM