I have integrated Facebook login into my application, but through testing I have found a bug and I am not sure whaat is the problem.
If my account for say is already logged into the device and I tap the button to login into Facebook, it will prompt the dialog "Continue As YOUR USERNAME" and it works fine.
But if I log out of my device on the phone and press the button to log-in using FB it will prompt me to login as which user, and after I login to a user I desire. After logging in it shows "This app has no key hashes configured etc..."
What could be the possible problem? Thanks in Advance! :D
This is my main activity class:
//Keys for Sharedpreferences
public static final String SHARED_PREF_NAME = "myloginapp";
public static final String EMAIL_SHARED_PREF = "email";
public static final String LOGGEDIN_SHARED_PREF = "loggedin";
private boolean loggedIn = false;
//FACEBOOK SDK
TextView text,userID,accessToken;
Button loginButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
//FACEBOOK SDK
text = (TextView) findViewById(R.id.text);
loginButton = (Button) findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
private String uid,email,name;
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
uid = object.getString("id");
email = object.getString("email");
name = object.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(LOGGEDIN_SHARED_PREF, true);
editor.putString(EMAIL_SHARED_PREF, email);
editor.commit();
Intent intent = new Intent(MainActivity.this, userPage.class);
intent.putExtra("username", email);
startActivity(intent);
finish();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
});
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("email"));
}
});
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
loggedIn = sharedPreferences.getBoolean(LOGGEDIN_SHARED_PREF, false);
if(loggedIn){
Intent intent = new Intent(MainActivity.this, userPage.class);
startActivity(intent);
}
}
