Android Facebook sdk can't handle device rotation properly? - android

I'd like to authenticate my users using the Facebook SSO service. I managed to handle basic login with the official Android Facebook sdk, which was quite painless, but it seems that the sdk just can't handle device rotation - at least I couldn't find a way to get it work correctly.
The auth code:
loginButtonFacebook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do FB login
facebook.authorize(LoginActivity.this, new DialogListener() {
#Override
public void onComplete(Bundle values) {
Toast.makeText(LoginActivity.this, "Facebook login successful", Toast.LENGTH_SHORT).show();
Log.d(TAG, "FACEBOOK LOGIN SUCCESSFUL");
//loginFB(facebook.getAccessToken());
}
#Override
public void onFacebookError(FacebookError error) {
Toast.makeText(LoginActivity.this, "FacebookError:" +error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
#Override
public void onError(DialogError e) {
Toast.makeText(LoginActivity.this, "Error: "+e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCancel() {
Toast.makeText(LoginActivity.this, "Cancelled!", Toast.LENGTH_SHORT).show();
}
});
}
});
My Activity also needs to handle the result from the FB login activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
This works, but the facebook.authorize(...) method need a reference for my current Activity, so it can do a callback later. But if the device is rotated when the user is in the facebook authorization Activity, my previous Activity gets destroyed. So after the user authorizes my app., I don't get the callback about this.
Has anyone found a solution for this?
Thanks

in your manifest file under your activity name just add the following line
android:configChanges="orientation"
inside your activity
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
................
}
Refer here and here

I usually hate recommending this, but the Activity that Facebook uses can be locked to portrait without any negative consequences. All it does is show an indefinite ProgressBar.
Check your manifest for the activity with the name com.facebook.LoginActivity and make sure it has the following properties:
<activity
android:configChanges="keyboardHidden"
android:name="com.facebook.LoginActivity"
android:screenOrientation="portrait" />

Related

How to Retrieve Login Callback from twitter webview using fabric

I am implementing twitter login using fabric, the login with native app seems fine. But when i am trying the login without twitter app installed in the device, it starts authorization with the webview, but on completion i didnt receive any callback neither in onSucccess or failure method
mTwitterAuthClient.authorize(ConnectSocialMedia.this, new Callback<com.twitter.sdk.android.core.TwitterSession>() {
#Override
public void success(Result<com.twitter.sdk.android.core.TwitterSession> result) {
// Success
mDialog.dismiss();
// The TwitterSession is also available through:
// Twitter.getInstance().core.getSessionManager().getActiveSession()
com.twitter.sdk.android.core.TwitterSession session = result.data;
ApplicationData.setSharedPrefValue(mActivity, "twittertoken", session.getAuthToken().token);
ApplicationData.setSharedPrefValue(mActivity, "twittertokensecret", session.getAuthToken().secret);
ApplicationData.setSharedPrefValue(mActivity, "twitterid", String.valueOf(session.getUserId()));
imgTwitter.setVisibility(View.VISIBLE);
mTwitterLoginButton.setEnabled(false);
btnClose.setVisibility(View.VISIBLE);
// getTwitterCover(String.valueOf(session.getUserId()));
}
#Override
public void failure(TwitterException e) {
mDialog.dismiss();
Log.d("TwitterKit", "Login with Twitter failure", e);
Toast.makeText(mActivity, "Twitter Login Failed", Toast.LENGTH_SHORT).show();
mTwitterLoginButton.setEnabled(true);
}
});
}
});
and neither on onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Make sure that the mTwitterLoginButton hears the result from any
// Activity that it triggered.
mTwitterAuthClient.onActivityResult(requestCode, resultCode, data);
}
Please help me, thanks
Add any call back Url in your Twitter app settings page. Every thing will work correctly after that. :)

FacebookCallback.onCancel is getting called when trying to login using facebook sdk

I have an Android app and I am trying to use Facebook's SDK (version 4.1.0) to get a token and log in. Here is my code:
public class LoginActivity extends Activity {
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Log.v(TAG, "Facebook login was successful");
String authToken = accessToken.getToken();
// User authToken here:
}
#Override
public void onCancel() {
Log.v(TAG, "Facebook login was canceled");
}
#Override
public void onError(FacebookException e) {
Log.e(TAG, "Facebook login failed: " + e.getMessage());
}
});
Button facebook_button = (Button) findViewById(R.id.fbButton);
facebook_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(getActivity(), Arrays.asList("public_profile"));
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
The code switched to the Facebook app and back and OnActivityResult() is called. However, every time the callback method that is called is onCancel(). Note that I am not using the LoginButton provided by Facebook, and I have my own button (although I tried that approach and the result was the same). I double and triple checked my app ID and the keyhash generated by the app and they look correct too. So, I don't know what else may be wrong. Any help at this point is greatly appreciated.
yes i was facing the same issue, resolved it by using the below code just before login
LoginManager.getInstance().logOut();
The reason behind this behaviour is that you are already logged in. So when you revoke it, oncancel() gets called instead of onsubmit(). So just perform logout on your application's logout button like this
Import -->
import com.facebook.login.LoginManager;
Implementation -->
LoginManager.getInstance().logOut();
I had the same issue, eventually I found the problem. The activity calling facebook login fragment had android:launchMode="singleInstance" in the manifest file.
#Override
public void onCancel() {
accessToken = AccessToken.getCurrentAccessToken();
if (accessToken == null) {
Toast.makeText(LoginActivity.this, "Login unSuccessful..Please contact developer... ", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LoginActivity.this, "Login Successful. ", Toast.LENGTH_LONG).show();
}
}
#Override
public void onError(FacebookException error) {
Toast.makeText(getApplicationContext(), "Login attempt failed.", Toast.LENGTH_SHORT).show();
}
List item
#Doru's comment lead me to the solution. I used the Facebook activity rather than fragment.
Instead of
<activity android:name="com.facebook.FacebookActivity"
android:screenOrientation="portrait"
android:launchMode="singleTask" />
I needed to write
<activity android:name="com.facebook.FacebookActivity"
android:screenOrientation="portrait" />
What's nasty about this bug, is that it only occurs on old Android versions (e.g. 4.4.2, not 5.0).
If anyone comes across this in the future, this can also be caused by the Facebook native app installed on the device blocking login due to reason X.
In my case, this was being thrown every single time I attempted login. I finally switched over to the native Facebook app and as it opened, it required authentication and had me log in again due to some 'suspicious activity' (I think I changed my number recently).
If you are doing everything that is listed above and users are still complaining about it (and they have the app installed, you can use the code at the bottom to see if the Facebook app in installed:) you may wish to include a dialog that asks them to check the Facebook app and make sure they are still logged in, which would then prompt them to fix any errors before tabbing back into your application.
//Code to check if Facebook app is installed:
public static boolean doesUserHaveFacebookAppInstalled(Context context){
try{
context.getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
return true;
} catch( PackageManager.NameNotFoundException e ){
return false;
}
}
The issue was simply because the APP ID needed to be in double-quotes.

Android facebook sdk login error 1118578

Everything was working fine, until I wanted to check what happens if the user clicks the Login with Facebook button with no internet connection available.
A toast showed up saying:
Login failed. Please contact the maker of this app and ask them to report issue #1118578 to Facebook.
In the logcat I have:
D/Facebook-authorize(2824): Login canceled by user.
I tried to get rid of this toast and display my own error message in the onCancel() method - but I can't find a way to disable that toast.
When I enabled WiFi again, single sign on didn't work anymore!
What could be the cause of this?
private void fbLogin() {
facebook.authorize(this, new String[] { "email" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {
app.save("fb_token", facebook.getAccessToken());
app.save("fb_expire", facebook.getAccessExpires());
mAsyncRunner.request("me", new meRequestListener());
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
EDIT:
To answer my own question, removing android:launchMode="singleInstance" From the manifest in the <activity> resolved the #1118578 issue.
However, I do need the android:launchMode="singleInstance" for the twitter OAuth login, as the browser passes the data back to the activity via new Intent. If I do not provide android:launchMode="singleInstance" in the manifest, a new activity is launched and it does not recieve the OAuth verifier.
Is there any way around this, to use Facebook and Twitter login in the same activity?
The only solution I think of is to use a dummy activity for Twitter4j with singleInstance.
My way to overcome this has been by changing the callback in twitter, from "twitter://callback" to "http://localhost/callback" and remove the android:launchMode="singleInstance" and the twitter intent-filter from the manifest.
I can still get the response in the WebView (rather than on the onResume() as before):
yourwebview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// your_callback == http://localhost/callback
if(url == null || !url.startsWith(your_callback))
return;
Uri uri = Uri.parse(url);
String ov = uri.getQueryParameter("oauth_verifier");
AccessToken at = null;
...

Android Facebook SDK authorize methods shows dialog every time

I'm writing an Android application that should upload photos to Facebook.
Everything works just fine except one thing. When I called the Facebook.authorize() method for the first time the Facebook dialog was shown with requested permissions and 'Allow'/'Don't Allow' buttons. That was OK. But when I run my app for the secong time I've got the same dialog, but with message that my application allowed already and suggestion to press OK button.
Is there a way to avoid this second dialog? Should I skip the authorize method in some conditions?
I tried to call the Facebook.isSessionValid() method before authorize method, but this did not help.
Here is my simplified code:
mFacebook = new Facebook(APPLICATION_ID);
mFacebookAsync = new AsyncFacebookRunner(mFacebook);
if (mFacebook.isSessionValid()) {
uploadPictureFile();
}
else {
mFacebook.authorize(this, new String[] {"publish_stream"}, new Facebook.DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
Toast.makeText(PhotoFeederFacebookSendActivity.this, "Facebook error: " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
finishWithResultCode(RESULT_CANCELED);
}
#Override
public void onError(DialogError e) {
Toast.makeText(PhotoFeederFacebookSendActivity.this, "Facebook dialog error: " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
finishWithResultCode(RESULT_CANCELED);
}
#Override
public void onComplete(Bundle values) {
uploadPictureFile();
}
#Override
public void onCancel() {Toast.makeText(PhotoFeederFacebookSendActivity.this, "Facebook authorization cancelled.", Toast.LENGTH_LONG).show();
finishWithResultCode(RESULT_CANCELED);
}
});
}
And here is my onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
Any help appreciated. Thanks.
You need to call mFacebook.setAccessToken("your token") before mFacebook.isSessionValid()
and you need to save the token to preference yourself
If you only intend to update status see my post here...
Android Facebook Graph API to update status

Unable to share Content on Facebook through Android App

I have an android app in which i am using Facebook Android SDK to post content from my application to Facebook Wall. My code was working perfectly till i updated the Facebook app on my phone. Since that time, whenever i try to post content on Facebook, i get the login screen with the "Loading" option and after a few seconds, nothing happens. Here is the code which i am using
On Facebook Button CLick
if(isOnline())
{
mFacebook = new Facebook("APP ID");
mFacebook.authorize(this,new String[] {"publish_stream", "read_stream", "offline_access"}, new AuthorizeListener());
}
The Code for Authorize listener is
class AuthorizeListener implements DialogListener {
public void onComplete(Bundle values) {
// Handle a successful login
postOnWall("My wall text");
}
#Override
public void onCancel() {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onFacebookError(FacebookError e) {
}
}
After debugging i found that the OnComplete method is not being called after calling the authorize function and there is also no exception. This was working perfectly till i update the Facebook app on my Android phone.
Any help will be appreciated
Make sure you are calling Facebook.authorizeCallback in your Activity's onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
// ... anything else your app does onActivityResult ...
}
For the details, carefully read this page.

Categories

Resources