Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for logout from Facebook in android

I am using customize Facebook login button .this is my code. I'm using session manager to store email and name for entire app.I have used logout button in third activity .

How to logout from Facebook

public void FB_Login(View view){

    callbackManager = CallbackManager.Factory.create();


    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));

       LoginManager.getInstance().registerCallback(callbackManager,
               new FacebookCallback<LoginResult>() {
                   @Override
                   public void onSuccess(LoginResult loginResult) {
//                       AccessToken.getCurrentAccessToken().getPermissions();
//                       Profile profile = Profile.getCurrentProfile();
//                       String firstName = profile.getFirstName();
//                       System.out.println(profile.getProfilePictureUri(20,20));
//                       System.out.println(profile.getLinkUri());

                       // App code
                       AccessToken.getCurrentAccessToken().getPermissions();

                       GraphRequest request = GraphRequest.newMeRequest(
                               loginResult.getAccessToken(),

                               new GraphRequest.GraphJSONObjectCallback() {
                                   @Override
                                   public void onCompleted(
                                           JSONObject object,
                                           GraphResponse response) {
                                       // Application code
                                       try {
                                           email = object.getString("email");

                                           name=object.getString("name");

// Creating user login session
                                           // For testing i am stroing name, email as follow
                                           // Use user real data
                                           session.createLoginSession(name, email);

                                           // Staring MainActivity
                                            if (session.isLoggedIn()) {
                                                in = new Intent(HomePageActivity.this, HomeSearchActivity.class);
                                                in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                                                // Add new Flag to start new Activity
                                                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                startActivity(in);

                                                finish();

                                            }
                                       } catch (JSONException e) {
                                           e.printStackTrace();
                                       }
                                       Log.v("LoginActivity", response.toString());
                                   }
                               });
                       Bundle parameters = new Bundle();
                       parameters.putString("fields", "id,name,email,gender, birthday");
                       request.setParameters(parameters);
                       request.executeAsync();

                   }

                   @Override
                   public void onCancel() {
                       // App code
                       Log.v("LoginActivity", "cancel");
                   }

                   @Override
                   public void onError(FacebookException exception) {
                       // App code
                       Log.v("LoginActivity", exception.getCause().toString());
                   }
               });


}

Anyone help me. please.

like image 932
Ravi Kumar Avatar asked Jan 07 '23 18:01

Ravi Kumar


2 Answers

You can logout from Facebook using Facebook Graph API.

LoginManager.getInstance().logOut();

I hope it helps!

like image 55
Rajesh Avatar answered Feb 20 '23 13:02

Rajesh


You need to use :

LoginManager.getInstance().logOut();
like image 24
Ravi Avatar answered Feb 20 '23 11:02

Ravi