1

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);
     }
atieh mokhtary
  • 253
  • 2
  • 5
  • 18

2 Answers2

2

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();
                 } 
         }
    };
atieh mokhtary
  • 253
  • 2
  • 5
  • 18
0

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

Community
  • 1
  • 1
Harvi Sirja
  • 2,472
  • 2
  • 18
  • 19
  • thanks a lot for your answer; the most is done with the code you gave but still exactly when user clicks logout, nothing happens and I should resume to that activity to disappear the textview. is there any way to write this code at onClick() method of logout? – atieh mokhtary Jul 25 '15 at 12:05
  • LoginManager.getInstance().logOut(); use this for logout. and check my answer here.It will helps you http://stackoverflow.com/questions/31100108/logout-feature-disable-in-facebook-android-integration/31354259#31354259 – Harvi Sirja Jul 25 '15 at 13:23
  • after a big delay I had I am back.. sorry but I don't have any problem to logout the facebook. as the facebook sdk does, after login button clicked, it's text changes to logout and when logout clicked app would logout from facebook. now I have this question that exactly when logout clicked, I want to run a method. I mean immediately when logout clicked , how can I call a method – atieh mokhtary Aug 01 '15 at 13:33