firebase / FirebaseUI-Android

Optimized UI components for Firebase
https://firebaseopensource.com/projects/firebase/firebaseui-android/
Apache License 2.0
4.63k stars 1.83k forks source link

Error in the sample app or a coding practice #1997

Open rawandtakna opened 3 years ago

rawandtakna commented 3 years ago

From the sample app, in the firestore chat app, here is the code in onStart() method

public void **onStart**() {
    super.onStart();
    if (isSignedIn()) {
        **attachRecyclerViewAdapter**();
    }
    FirebaseAuth.getInstance().addAuthStateListener(this);
}

and in onAuthStateChanged

@Override
public void **onAuthStateChanged**(@NonNull FirebaseAuth auth) {
    mBinding.sendButton.setEnabled(isSignedIn());
    mBinding.messageEdit.setEnabled(isSignedIn());

    if (isSignedIn()) {
        **attachRecyclerViewAdapter**();
    } else {
        Toast.makeText(this, R.string.signing_in, Toast.LENGTH_SHORT).show();
        auth.signInAnonymously().addOnCompleteListener(new SignInResultNotifier(this));
    }
}

The method attachRecyclerViewAdapter() gets called twice every time the app is opened.

thatfiredev commented 2 years ago

@rawandtakna nice catch! I would suggest removing the if check from onStart and only keep addAuthStateListener(). Would you like to send a PR?