I am trying to add a Facebook login button in the main activity of a simple application. When I press the "login with Facebook" button, I have the FB application open for login, but after the successful login, the app comes back to the main screen of my application and nothing happens (the callback function registered for the LoginManager is not called).
My code looks like:
public class MainActivity extends FragmentActivity {
Button changeButton;
LoginButton loginButton;
CallbackManager callbackManager;
MainActivity thisActivity;
// Sets the Facebook login.
private void setupFacebookLogin() {
// Create the callbackManager and add functionality to it.
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
throw new AssertionError();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thisActivity = this;
// Initialize Facebook SDK.
FacebookSdk.sdkInitialize(getApplicationContext());
setupFacebookLogin();
// Set the current layout to main.
setContentView(R.layout.activity_main);
}
@Nullable
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
return super.onCreateView(name, context, attrs);
}
@Override
protected void onResume() {
super.onResume();
AppEventsLogger.activateApp(this);
}
@Override
public void onPause() {
super.onPause();
AppEventsLogger.deactivateApp(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
Also, my xml is:
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change"
android:id="@+id/button1"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="61dp" />
<com.facebook.login.widget.LoginButton
android:id="@+id/fb_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true" />
I would expect the application to crash when it returns from FB login, due to the assertion in onSuccess. However, it is not the case. Any help would be appreciated, thank you!
Add the following line in "Login with Facebook" button click listener:
LoginManager.getInstance().logInWithReadPermissions(WelcomeActivity1.this, (Arrays.asList("public_profile", "user_friends","user_birthday","user_about_me","email")));