Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot logout from another activity (Facebook - Android)



I'm creating an app and I successfully resolved logging in with Facebook using Facebook SDK 4.7.0. I'm using LoginManager (not LoginButton) as I want to have a custom button for this.
The flow is like this:

  • Open the app and greeted by MainActivity
  • Click START button and opens the SignInActivity where you click the Sign In with Facebook Button, logs you and and closes this activity
  • Then you are again at the MainActivity screen
  • In the navigation drawer slider I have a LOGOUT button at the bottom, I initialized it and I display it only when you are logged in i.e. when the user_id of your Facebook profile is not null.

I implemented the following on the Logout Button onClickListener:

FacebookSdk.sdkInitialize(getApplication().getApplicationContext()); LoginManager.getInstance().logOut();

But when I press the button on my device it doesn't do anything (shows only ViewPostImeInputStage ACTION_DOWN in logcat, which is standard for a button press.

I saw other posts on SO in which this method worked so I don't understand why it won't work in my case.

SignInActivity.java

    FacebookSdk.sdkInitialize(getApplication().getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.e(TAG, "User ID: " + loginResult.getAccessToken().getUserId());
            Log.e(TAG, "Auth Token: " + loginResult.getAccessToken().getToken());
            SharedPreferences prefs = getSharedPreferences("com.dotfreeride.dotfreeride.login", 0);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("userId", loginResult.getAccessToken().getUserId());
            editor.putString("fbToken", loginResult.getAccessToken().getToken());
            editor.commit();
        }

        @Override
        public void onCancel() {
            Toast.makeText(SignInActivity.this, "Login attempt canceled!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException error) {
            Toast.makeText(SignInActivity.this, "Login attempt failed!", Toast.LENGTH_SHORT).show();
        }
    });

MainActivity.java

logoutBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FacebookSdk.sdkInitialize(getApplication().getApplicationContext());
                LoginManager.getInstance().logOut();
            }
        });
like image 782
Raul Bărnuțiu Avatar asked Nov 09 '22 01:11

Raul Bărnuțiu


1 Answers

This worked for me

public class Profile extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);


}

public void gotoLogin(View view){
    FacebookSdk.sdkInitialize(getApplicationContext());
    LoginManager.getInstance().logOut();
    Intent intent = new Intent(this, Login.class);
    startActivity(intent);
}

}

I had the same problem because i extented the AppCompatActivity. With ActionBar it works. Hope it will do the work for you :)

like image 129
Iraklis Bekiaris Avatar answered Nov 15 '22 11:11

Iraklis Bekiaris