I am creating an application and I have successfully enabled Facebook login using the Facebook SDK 4.7.0. I use LoginManager (not LoginButton) since I want to have a custom button.
The stream is as follows:
- Open the app and welcome MainActivity
- Press the "START" button and open the "SignInActivity", where you click the "Log in using the Facebook button" button, it logs and closes this activity.
- Then you are back on the MainActivity screen.
- In the navigation box slider, I have a LOGOUT button at the bottom, I initialized it, and I only show it when you are logged in, that is, when the user_id of your Facebook profile is not
null .
I implemented the following on the Logout onClickListener button:
FacebookSdk.sdkInitialize(getApplication().getApplicationContext()); LoginManager.getInstance().logOut();
But when I press the button on my device, it does nothing (it only shows ViewPostImeInputStage ACTION_DOWN in logcat, which is the standard for the button to be pressed.
I saw other posts on SO where this method worked, so I donโt understand why it will not 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(); } });
source share