Can't get CallbackManager to be fired - android

I am working on FB Login integration on my app. I have done all the set-up neccessary and generated my hash-key. I use a custom UI view for the login so I implement the LoginManager in my activty as below
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this);
setContentView(R.layout.activity_account_front);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// not called
Log.e("fb_login_sdk", "callback success");
}
#Override
public void onCancel() {
// not called
Log.e("fb_login_sdk", "callback cancel");
}
#Override
public void onError(FacebookException e) {
// not called
Log.e("fb_login_sdk", "callback onError");
}
});
final Activity activity = this;
face = (ImageView) findViewById(R.id.face);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("fb_login_sdk", "click");
List<String> perm = new ArrayList<String>();
perm.add("email");
LoginManager.getInstance().logInWithReadPermissions(activity, perm);
}
});
With this, the highest I have gotten is the onclick on the imageview log. I have implemented this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("Results", String.valueOf(resultCode));
callbackManager.onActivityResult(requestCode, resultCode, data);
}
but none of the logs are printed apart from the one at the beginning of the click event. When I press the button, a spinner shows briefly and then the app just end (not crash). How do I get this to work, its driving me crazy as I have spent about a day tring to get this to work. Thanks

Try to use application context instead of activity context when you initialize facebook sdk.
Replace this
FacebookSdk.sdkInitialize(this);
with
FacebookSdk.sdkInitialize(getApplicationContext());
Hope it will be useful for you.

For me, it was a simple and perhaps an oversight. As #Arkar pointed out, I forgot to remove this from my AndroidManifest file for that particular Activity and hence there was not callback triggered.
android:noHistory

Related

Setup multiple facebook LoginButton in multiple activity

I'm dealing with this strange behavior where the same code, used in 2 different activities, is producing a different output.
The code is the standard facebook sdk login iteration, so:
private CallbackManager fbCallbackManager;
private LoginButton signFacebookButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(context);
fbCallbackManager = CallbackManager.Factory.create();
setContentView(R.layout.sign);
signFacebookButton = (LoginButton) findViewById(R.id.sign_fb_button);
signFacebookButton.setReadPermissions("public_profile");
signFacebookButton.setReadPermissions("email");
signFacebookButton.registerCallback(fbCallbackManager, fbCallback);
}
private FacebookCallback<LoginResult> fbCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
Log.d("Facebook Callback", "success");
}
#Override
public void onCancel() {
Log.d("Facebook Callback", "cancel");
}
#Override
public void onError(FacebookException e) {
Log.d("Facebook Callback", "error");
}
};
This same exact code used in two different activities is behaving differently.
In the activity where is it first called, it works properly, but if I skip the first activity (so I don't click the login button), but then click a new login button in a second activity, the code is simply not working. It show the facebook progress bar for a couple of seconds, then nothing happen (nor any of the log is printed in the console). Any help would really be appreciated.
After half a day wasted behind this, I noticed that I was missing the actual fbcallbackManager call in onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
this.fbCallbackManager.onActivityResult(requestCode, resultCode, data);
}

Facebook login not logging in after log out Android

I am building an android application that uses the Facebook SDK. I am currently using the LoginManager to handle the login flow.
I have already initialised the Facebook sdk in the Applications onCreate method.
public class DummyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
FacebookSdk.sdkInitialize(this);
AppEventsLogger.activateApp(this);
}
}
I have also setup my LoginActivity as such:
public class LoginActivity extends AppCompatActivity {
#Bind(R.id.custom_login_button)
Button mCustomFacebookButton;
private CallbackManager callbackManager;
private LoginManager mLoginManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
callbackManager = CallbackManager.Factory.create();
mLoginManager = LoginManager.getInstance();
mLoginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
error.printStackTrace();
}
});
mCustomFacebookButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAuthProgressDialog.show();
mLoginManager.logInWithReadPermissions(LoginActivity.this
, Arrays.asList("user_events", "email" ,"user_friends"));
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
The flow is that once the login button is pressed, it will start the MainActivity, and in that activity there is a SettingsActivity with a Logout Button that calls
LoginManager.getInstance().logOut();
which will take you back to the login activity.
The problem that I am facing is that once the logout is called, I can't log back in, and my AccessToken is always null. So in essence I am stuck on the LoginActivity.
It gets weirder, once I delete the build files and reimport the project, it works fine, until I log out again and this problem starts again. It has only been having this problem since I updated to Android Studio 2.1, build tools to 23.0.3, and SDK tools to 25.1.3.
-Edit: its not correcting itself with a re-import anymore
So it turns out that in my manifest for my LoginActivity I had noHistory as true. This led to my onActivityResult not being called. Once I removed this line the problem relating to the Facebook login disappeared.

registerCallback method for a custom button - facebook sdk

I would like to get user profile data;
I'm following this tutorial :
http://code.tutsplus.com/tutorials/quick-tip-add-facebook-login-to-your-android-app--cms-23837
The above tutorial uses registerCallback method like this:
private LoginButton loginButton;
.
.
.
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
now, How can I implement this method for a custom Button ?
below link use Facebook class, but for me this class undefined!
// Instance of Facebook Class
private Facebook facebook;
http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/
You can use LoginManager to accomplish it without use the LoginButton.
First, you have to create the CallbackManager as usual:
callbackManager = CallbackManager.Factory.create();
Next, you have to register the callback created with LoginManager instance:
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
And finally, you have to invoke the Facebook login with some permissions, for example:
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email"));
Don't forget override the onActivityResult method and call onActivityResult method from the CallbackManager:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
The first step was to include the built in facebook button in my xml file but set its visibility to "gone" so it cannot be seen by the user.
<com.facebook.widget.LoginButton
android:id="#+id/authButton"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
/>
In my main activity, I then defined the hidden facebook button and the button I wanted to use
fbLoginButton = (Button) findViewById(R.id.authButton); //built in facebook button
customButton = (Button) findViewById(R.id.customButton); //my custom button
Then in an onClickListener for the custom button, I used the ".performClick" method on the facebook button
customButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fbLoginButton.performClick();
}
});
This works like a charm for me. Hopefully this can help somebody.
#Rich Luick tnx.
I give step by step code for make facebook button custom. check code here
you just have to make a frame layout.

Login callback is not triggered using facebook-android-sdk 4

I have an activity for user to login with facebook. I used facebook-android-sdk v4.0.0. But login callback is not triggered when user click on login button. After showing progress bar, start previous activity automatically without showing any error on log instead of triggering login callback.
In SignUpActivity,
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_btn);
loginButton.setReadPermissions(Arrays.asList("user_friends", "email"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.i("Login : ", "Success");
}
#Override
public void onCancel() {
Log.i("Login : ", "Cancel");
}
#Override
public void onError(FacebookException e) {
Log.e("Login Error : ", e.getMessage() + "");
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
I also added meta-data element to application element and the FacebookActivity to the manifest. Moreover, finished initializing facebook SDK in Application :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
}
What's wrong with my code ? Thanks for any suggestion !!
Edit
I found myself what's wrong with my code. I start SignUpActivity with no_history flag. After I remove that flag, everything is okay now.

Android Facebook SDK 4.0.0 Sharing callback doesn't work properly

I am using following code to share link on facebook. when user click on cancel on Share dialog interface,onSuccess() callback method is called sometimes instead of onCancel(). And getting post id null.Please help me what's going wrong?
ShareButton btn;
CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this);
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_share);
btn = (ShareButton) findViewById(R.id.btn_share);
btn.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.e("Tag","Successfully posted");
Log.e("Post id",result.getPostId());
}
#Override
public void onCancel() {
Log.e("Tag","Canceled by user");
}
#Override
public void onError(FacebookException error) {
Log.e("Tag",error.getLocalizedMessage());
}
});
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("My Custom URL"))
.setContentTitle("Test")
.build();
btn.setShareContent(content);
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Well, maybe I'm late but I faced the same issue weeks ago.
What I've noticed is if you press Cancel in the web-base Share dialog, the onSuccess() method is called but the Share.Result object contains a null postId, so you can control whenever the user pressed cancel or share by checking the Share.Result response.
Another thing I've noticed is that if you share content with native app installed, postId field is always null... so you will have to check if the user has the app installed to check or not the postId field.

Categories

Resources