Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to logout in facebook from another activity?

I am using facebook SDK 4.4.0. i want to logout in facebook from another activity? How can I add logout functionality in another activity ? I want to go to another activity after successful login and add logout functionality in that activity

loginButton.registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        new fblogin().execute(loginResult.getAccessToken());

                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onError(FacebookException e) {

                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    class fblogin extends AsyncTask<AccessToken, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Wait.....");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(AccessToken... params) {
            GraphRequest request = GraphRequest.newMeRequest(params[0],
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object,
                                GraphResponse response) {

                            Log.v("MainActivity", response.toString());

                            try {

                                username = object.getString("first_name");
                                emailid = object.getString("email");
                                gender = object.getString("gender");

                                Log.v("User Name", username);
                                Log.v("Email", emailid);
                                Log.v("Gender", gender);

                            } catch (JSONException e) {
                                // TODO Auto-generated catch
                                // block
                                e.printStackTrace();
                            }
                        }

                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields",
                    "id,first_name,email,gender,birthday");
            request.setParameters(parameters);
            request.executeAndWait();

            return null;
        }

        protected void onPostExecute(String file_url) {

            pDialog.dismiss();

        }

    }
like image 288
mInE..... Avatar asked Jan 07 '23 20:01

mInE.....


1 Answers

First initialize the FacebookSdk.sdkInitialize(getApplicationContext()); on your activity then write the below code on button click event:

LoginManager.getInstance().logOut();

It is done!!!

like image 150
rushank shah Avatar answered Jan 19 '23 03:01

rushank shah