13

This code is working well when I uninstalled the Facebook App but didn't work with Facebook App installed. I'm using Facebook SDK 4.0.

This is my code

package com.example.nhp04.gqfood;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;



public class Login extends AppCompatActivity implements Animation.AnimationListener {

private String info = "";
private LoginButton loginButton;
private CallbackManager callbackManager;
private AccessTokenTracker tracker;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_login);
loginButton = (LoginButton)findViewById(R.id.login_button);



loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
            info = ("User ID: " + 

    loginResult.getAccessToken().getUserId() + "\n" + "Auth Token: " + loginResult.getAccessToken().getToken());
                }

                @Override
                public void onCancel() {
                    info = ("Login attempt canceled.");
                }

                @Override
                public void onError(FacebookException e) {
                    info = ("Login attempt failed.");
                }
            });
            System.out.println(info);
            tracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

            }
        };
        tracker.startTracking();
    }
    }

this function for checking login

public boolean isLoggedIn() {
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    return accessToken != null;
}

this on Resume and on Stop methods

@Override
protected void onResume() {
    super.onResume();
    if (isLoggedIn()){
        Intent home = new Intent(this, home.class);
        startActivity(home);
    }
}

@Override
protected void onStop() {
    super.onStop();
    tracker.stopTracking();
    finish();
}

And this is my onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Intent home = new Intent(this, home.class);
        startActivity(home);
    } else {
        Toast.makeText(getApplicationContext(), "Unable to login please check your internet connection",Toast.LENGTH_LONG).show();
    }
}
Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43

4 Answers4

4

where is your onActivityResult() code. In onActivityResult() you need to use callbackmanager. User below code:

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);

}

above will work both in fragment/activity. Make sure you have

1. facebook app installed on your testing device
2. In facebook developer account check whether you have mentioned 
- correct package name : refer your android project manifestfile.xml

- check that have you mentioned correct launcher class
- Check that you have given correct debug/release hash key

3. Cross check your facebook application id and that mentioned in your manifestfile.xml facebook meta data are same

In your code change below

create you callbackmanager after setContentView(...);

change it to below FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); setContentView(R.layout.activity_login); callbackManager = CallbackManager.Factory.create();

Remember if this is with you facebook issue then your problem lies within this dont waste time in searching other thing. Also put log in failure method in callback of facebook sdk.

Post comment if you still have problem

Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43
MobDev
  • 464
  • 4
  • 14
  • place your callbackmanager after setContentView in onCreate method and try again – MobDev May 05 '16 at 10:07
  • do checks for 1.facbook app id in manifestfile as in your fb developer account 2. check your package name and MainActivity mentioned in fb dev account is correct 3.Check you have added proper hash key based on debug or release keystore – MobDev May 05 '16 at 10:12
  • 1
    implement @Override public void onError(FacebookException error) { } – MobDev May 05 '16 at 10:14
  • 1
    implement onError in fb registercallback and check what error message it is showing – MobDev May 05 '16 at 10:15
  • also check log.. it will giving some message from facebook with some error – MobDev May 05 '16 at 10:15
  • check the code i already add the onError and onCancel methods – Yaseen Ahmad May 05 '16 at 10:18
  • ok here is an update: Your android code is good. I did hands on with it. I was able to login. but check your facebook app id and manifestfile.xml file settings – MobDev May 05 '16 at 10:59
  • 1
    yes!!! needs to override onActivityResult to get login callback working. Thanks you saved my day! – mili Oct 25 '17 at 09:59
0

you can remove your app in your facebook app. like you can open facebook app in go setting>>account setting>>app>>youer App >> remove . after remove youer app it uninstall your app .and reinstall it and check login with facebook is working or not.

0

Please change your facebook sdk version, after you clean and rebuild your application it will work

Krishnan
  • 428
  • 1
  • 4
  • 11
0

if you're getting an error message that looks like this: Error

Invalid key hash. The key hash "...5GAvm/gHi..." does not match any stored key hashes. Configure your app key hashes at https://developers.facebook.com/apps/...55.../

then copy the hash key provided in the error message and replace the old one with it and try again with the Facebook app installed on your device

Chidi
  • 189
  • 2
  • 10