Android Facebook [4.10.0] How capture when user clicks logout button? - android

I've been searching around for the actual logout event button when logging out of facebook. I saw many people answering with this single line:
LoginManager.getInstance().logOut();
But I'm unsure on how to use that.
I am looking for the trigger action that actually logs a user out of facebook. Currently, I am using the LoginManager from facebook's Android SDK to log in.
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
final AccessToken accessToken = loginResult.getAccessToken();
GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject user, GraphResponse graphResponse) {
ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.facebook_profile_1);
facebook_id = user.optString("id");
facebook_name.setText(user.optString("name"));
facebook_name.setVisibility(View.VISIBLE);
profilePictureView.setProfileId(user.optString("id"));
}
}).executeAsync();
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Facebook Login Cancelled", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), "Facebook Login Error", Toast.LENGTH_LONG).show();
}
});
This works fine when the button is ready for login. I'm able to do whatever I'd like in the onCompleted part of that code.
But since facebook does its own thing with its button and changes the single button from "Log In" to "Log Out", how do I change things when the user logs out. To be clear, The user is able to log out, but I want to change things on the screen when this happens (for example, I want to make the name go away and the picture go away)?
When a user clicks "Log Out", a prompt comes up asking if they're sure they want to log out, then when the user clicks "Yes, I'm sure", I want to control parts of my app at that point.
Any thoughts?

I think you want to handle the logout callback and want to implement some coding when user logout you will find the answer here .

Related

Using Facebook android SDK ShareDialog. Clicking cross button of sharedialog returning to application with onSuccess callback

I am using Facebook Android SDK version 4.25 and ShareLinkContent, ShareDialog for posting to Facebook on user behalf using my app.
This is the dialog which opens when user clicks my app's share functionality
There are two cross icons in sharedialog as you can see in the image. When i am clicking upper cross icon, sharedialog closes and onCancel callback is called.
Problem is when i am clicking lower cross icon onSuccess callback is being called same when i am clicking Post button. I want to give user some points when he shares post to Facebook. But i am getting success callback on lower cross icon as well. What to do? Thank You.
This is the callback code i am using :-
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.e(LOG_TAG, "Facebook share success " + result.getPostId());
}
#Override
public void onCancel() {
Log.e(LOG_TAG, "Facebook share cancel");
}
#Override
public void onError(FacebookException error) {
Log.e(LOG_TAG, "Facebook share error");
}
});
When you click the upper cross, you are actually closing your own dialog box, that calls onCancel. But when click on the lower cross, it actually is in facebook view. So the callback is returned from facebook not your dialog. Try:
#Override
public void onSuccess(Sharer.Result result) {
Log.e(LOG_TAG, "Facebook share success " + result.getPostId());
//Notice this post id in both, cross click and when the status is updated successfully
//So, if you don't get a valid post id returned, you can count it as a cancel.
}

Android Facebook login always ask for user after logout

I am implementing a Facebook login with Facebook SDK on Android.
compile 'com.facebook.android:facebook-android-sdk:4.+'
I'm logging in with the user as
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
..
}
And for log out, I use my own log out button and log out the user programmatically:
LoginManager.getInstance().logOut();
My question is:
After log out, when I click on Continue with Facebook, the previous user information pops up to Continue as XY. I don't want this. I want to ask for email and password again, every time, if somebody wants to log in after log out. How can I do this?
Actually i found the solution. I changed the login behavior for the FB login button, for this i used:
loginButton.setLoginBehavior(LoginBehavior.WEB_ONLY);
So every time it pops up the WEB view for login button.
I want to ask for email, password again every time if somebody wants
to log in after log out.
try this in your onCreate()
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
updateWithToken(AccessToken.getCurrentAccessToken()); //add this method
Now in updateWithToken() method logout the user from previous session
private void updateWithToken(AccessToken currentAccessToken) {
if (currentAccessToken != null) {
LoginManager.getInstance().logOut();
} else {
}
}
EDIT
if you want to completely disconnect user from facebook login use:
public void disconnectFromFacebook() {
if (AccessToken.getCurrentAccessToken() == null) {
return; // already logged out
}
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
}
}).executeAsync();
}
To really disconnect from facebook with the android SDk and avoid auto login after you must use
val accessToken = AccessToken.getCurrentAccessToken()
val request = GraphRequest.newDeleteObjectRequest(accessToken, "me/permissions", { response ->
LoginManager.getInstance().logOut() //not really needed i think
})
request.executeAsync()
In response you have a responseCode == 200 if OK

android app integration with facebook review compulsory?

I have written a code to login using facebook button sdk 4.0. I am able to login/logout. There is an activity for user from where user can share their locally saved picture to facebook. Sharing is possible if native facebook app is installed. But not in case facebook is not installed. So I tried to use ShareApi for sharing pictures via web view. But it requires publish actions permission. So I just written following code to get permission. It opens a window where it ask for per ok and cancel with my app name. After pressing ok button it always falls in onCancel case automatically.
I have read somewhere that you need to submit your app for review to facebook team in order to take publish actions permission. Is this only reason i am not getting success after trying almost 18 hours.?? Please help me friends.
List<String> permissionNeeds = Arrays.asList("publish_actions");
LoginManager loginManager = LoginManager.getInstance();
loginManager.logInWithPublishPermissions(activity,permissionNeeds);
loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess" + loginResult);
sharePhotoToFacebook();
}
#Override
public void onCancel() {
System.out.println("onCancel");
}
#Override
public void onError(FacebookException error) {
System.out.println("onError"+error);
}
});

Disable auto log in with Facebook loginbutton

I have an Android application that uses Facebook sdk to log in. To do so, I use the LoginButton widget. The log in process works very well but I got a small problem : when I close the application or reinstall it, it automatically logs in into Facebook (the text on button switches to "Log out"). I don't want to have such behavior : I need that the user clicks on the button to log in every time the application starts. I checked on the internet and it seems this feature is called "single sign on" but I'm not sure about that. I have found several ways to do so but none of them works. I used Facebook SDK 4. This is the part of the code that instantiates the Activity.
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
this.setContentView(R.layout.login_layout);
getSupportActionBar().hide();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build();
mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
/*accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
updateWithToken(newAccessToken);
}
};*/
LoginManager.getInstance().logInWithReadPermissions(LogInActivity.this, Arrays.asList("public_profile", "user_birthday", "email"));
LoginManager.getInstance().setLoginBehavior(LoginBehavior.SUPPRESS_SSO);
fb_login = ((LoginButton) findViewById(R.id.fb_login));
fb_login.setLoginBehavior(LoginBehavior.SUPPRESS_SSO);
fb_login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.i("Facebook connect", "Connection success");
}
#Override
public void onCancel() {
Log.i("Facebook", "Super c'et cancel");
((TextView) findViewById(R.id.account)).setText("Annulé");
}
#Override
public void onError(FacebookException error) {
Log.i("Facebook", "Super y a une erreur");
((TextView) findViewById(R.id.account)).setText("Erreur");
}
});
Does someone have an idea how to disable to automatic connection ?
For Facebook SDK 4 and onwards, you could programmatically logout using:
LoginManager.getInstance().logOut();
Now, the problem is where do you place this code. If you're fine with Facebook logout when the app is sent to the background, you can use onPause() or onStop() methods.
The big part comes. You can't determine if the app is killed. Because it's simply killed and you don't get any callbacks to catch that event.
So, if you anyway want to implement this every-time-login thing, you have to do this when the app starts.
Initialize the Facebook SDK:
Check if the user is logged in.
If yes in step 2, run manual logout.
FacebookSdk.sdkInitialize(getApplicationContext());
if(AccessToken.getCurrentAccessToken() != null) {
LoginManager.getInstance().logOut();
}
step 1: like was written before you should make
LoginManager.getInstance().logOut();
step 2: To remove autologin just avoid behavior of Facebook app. You could do it via LoginBehavior.
LoginManager.getInstance().setLoginBehavior(LoginBehavior.WEB_ONLY);
Remove this line from your code
LoginManager.getInstance().logInWithReadPermissions(LogInActivity.this, Arrays.asList("public_profile", "user_birthday", "email"));

Not able to get profile details from Facebook on Android

I want to get the profile details from Facebook. I am using the latest Facebook SDK. I want to use custom login button to login and get profile details of user.
In onSuccess method i am getting LoginSuccess object . Now i am stuck how i got user profile data . And this code is on facebookButton not on custom button. Please give me some ideas to integrate the SDK via custom button. I go through the facebook documentation but do not got anything useful.
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
}
);
You can get simple user info (like name and id) by using in onSuccess() loginManager method Profile class:
Profile.getCurrentProfile() //get current instance of logged profile
Profile.getCurrentProfile().getId() //get current id
Profile.getCurrentProfile().getName() //get current user name
If you want to fetch more information about user, like email or date of birth, you should to use GraphAPI request. You can read about it in related question answer.
It doesn't matter, if you are using Custom Login button or not. In both situations SDK is using loginManager.

Categories

Resources