I can't figure out why i need to tap twice on a "submit" button. I have a simple form with 2 TextInputLayouts and a button that calls some logic to be executed, but it gets executed only when i press that button to times.
Follows the code:
XML
<Button
android:id="@+id/accedibtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pwDimenticataTextViewAccedi"
android:layout_centerInParent="true"
android:layout_marginTop="35dp"
android:text="@string/access"
android:textColor="@color/black"
android:textSize="14sp"
tools:ignore="TextSizeCheck" />
Listener in onCreate method
accedi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkUser(view);
if (account != null){
Routing.dashboardActivity(Login.this, account);
}
}
});
CheckUser Function
private void checkUser(View v){
String emailString = String.valueOf(email.getText());
String passwordString = String.valueOf(password.getText());
db.getCOLLECTION_ACCOUNT().whereEqualTo(DatabaseConstants.KEY_ACCOUNT_EMAIL, emailString)
.get().addOnSuccessListener(queryDocumentSnapshots -> {
List<DocumentSnapshot> document = queryDocumentSnapshots.getDocuments();
if(document.size()==1) {
String pwCheck = (String) document.get(0).get(DatabaseConstants.KEY_ACCOUNT_PASSWORD);
if (passwordString.equals(pwCheck))
account = document.get(0).toObject(Account.class);
}
}).addOnFailureListener(e -> {
Toast.makeText(Login.this, Testi.toastError, Toast.LENGTH_SHORT).show();
Log.e(TAG, e.toString());
});
}
Probably the checkUser function needs to be in another class, I don't know. in any case, I expect that when i tap on button this code will be executed, but it doesn't.
Probably is a simple question, but I have been programming on android for only 2 months
I don't know exactly what kind of a database you are using (maybe Firestore, not important), but .get()
seems to be an asynchronous operation as you have the OnSuccessListener
and OnFailureListener
to handle the outcome.
So, the flow in your app is now:
if (account != null){...}
account
is still null at that pointaccount
is no longer nullaccount != null is true
Maybe the OnSuccessListener
should do the Routing.dashboardActivity()
?
Anyway, you'll need to slightly re-think your application logic.