millinet millinet - 5 months ago 106
Android Question

New Firebase Version - Android - Facebook login won't work

So here is what I'm stuck with for more than one day now.
I'm trying to implement the new Firebase specs to login users using Facebook into my Android project.

I really thank in advance anyone who might be able to help me figuring out what's wrong.

Error I get again and again

The activity is indeed in the manifest, I can launch it, but when I click on the login button, I get the following error (What's weird is that it mentions FacebookActivity and not FacebookLoginActivity, which is the name of my activity):

06-10 11:38:49.058 7210-7210/com.yatoo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yatoo, PID: 7210
Log in attempt failed: FacebookActivity could not be started. Please make sure you added FacebookActivity to the AndroidManifest.
at com.facebook.login.LoginManager.startLogin(LoginManager.java:369)
at com.facebook.login.LoginManager.logInWithReadPermissions(LoginManager.java:263)
at com.facebook.login.widget.LoginButton$LoginClickListener.onClick(LoginButton.java:737)
at com.facebook.FacebookButtonBase$1.onClick(FacebookButtonBase.java:359)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10814)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)


Double-checked, in the manifest

<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<activity
android:name=".activity.FacebookLoginActivity" />


The FacebookLoginActivity code
Which is totally similar to the example providen by Google and that can be found here: Google Link to Facebook Login integration using the new Firebase version

public class FacebookLoginActivity extends AppCompatActivity implements
View.OnClickListener {

private static final String TAG = "FacebookLogin";

private TextView mStatusTextView;
private TextView mDetailTextView;

// [START declare_auth]
private FirebaseAuth mAuth;
// [END declare_auth]

// [START declare_auth_listener]
private FirebaseAuth.AuthStateListener mAuthListener;
// [END declare_auth_listener]

private CallbackManager mCallbackManager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_facebook_login);

// Views
mStatusTextView = (TextView) findViewById(R.id.status);
mDetailTextView = (TextView) findViewById(R.id.detail);
findViewById(R.id.button_facebook_signout).setOnClickListener(this);

// [START initialize_auth]
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// [END initialize_auth]

// [START auth_state_listener]
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// [START_EXCLUDE]
updateUI(user);
// [END_EXCLUDE]
}
};
// [END auth_state_listener]

// [START initialize_fblogin]
// Initialize Facebook Login button
mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}

@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}

@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
// [START_EXCLUDE]
updateUI(null);
// [END_EXCLUDE]
}
});
// [END initialize_fblogin]
}

// [START on_start_add_listener]
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
// [END on_start_add_listener]

// [START on_stop_remove_listener]
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
// [END on_stop_remove_listener]


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}

// [START auth_with_facebook]
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
// [START_EXCLUDE silent]
// [END_EXCLUDE]

AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(FacebookLoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}

// [START_EXCLUDE]
// [END_EXCLUDE]
}
});
}
// [END auth_with_facebook]

public void signOut() {
mAuth.signOut();
LoginManager.getInstance().logOut();

updateUI(null);
}

private void updateUI(FirebaseUser user) {
if (user != null) {
mStatusTextView.setText(getString(R.string.facebook_status_fmt, user.getDisplayName()));
mDetailTextView.setText(getString(R.string.firebase_status_fmt, user.getUid()));

findViewById(R.id.button_facebook_login).setVisibility(View.GONE);
findViewById(R.id.button_facebook_signout).setVisibility(View.VISIBLE);
} else {
mStatusTextView.setText(R.string.signed_out);
mDetailTextView.setText(null);

findViewById(R.id.button_facebook_login).setVisibility(View.VISIBLE);
findViewById(R.id.button_facebook_signout).setVisibility(View.GONE);
}
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_facebook_signout:
signOut();
break;
}
}
}

Answer

You should also define default FacebookActivity in your manifest like

<activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize"
        android:label="@string/app_name"/>
Comments