gpt4 book ai didi

java - Firebase Auth 在单击按钮时未启动 Activity 但在关闭并重新打开应用程序时有效

转载 作者:行者123 更新时间:2023-11-30 10:13:16 24 4
gpt4 key购买 nike

点击按钮时,Firebase Auth 未在 HomeActivitiy 上启动。

它仅在重新打开应用程序时有效。

public class Auth1Activity extends AppCompatActivity {

String string_1;
String string_2;

private static final Boolean CHECK_EMAIL_VERIFIED = false;
private static final String TAG = "LoginActivity";

private FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener authStateListener;
private FirebaseUser firebaseUser;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth_1);
firebaseAuth = FirebaseAuth.getInstance();

authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
}
};

firebaseUser = firebaseAuth.getCurrentUser();

if (firebaseUser != null) {
Intent intent = new Intent(Auth1Activity.this, HomeActivity.class);
startActivity(intent);
finish();
}

final EditText auth_layout_1_edit_text_1 = findViewById(R.id.auth_layout_1_edit_text_1);
final EditText auth_layout_1_edit_text_2 = findViewById(R.id.auth_layout_1_edit_text_2);
Button auth_layout_1_button = findViewById(R.id.auth_layout_1_button);

auth_layout_1_button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
string_1 = auth_layout_1_edit_text_1.getText().toString();
string_2 = auth_layout_1_edit_text_2.getText().toString();

if (string_1.matches("") && string_2.matches("")) {
auth_layout_1_edit_text_1.setError("Enter Email Address");
auth_layout_1_edit_text_2.setError("Enter Password");
} else if (!string_1.matches("") && !string_2.matches("")) {
firebaseAuth.signInWithEmailAndPassword(string_1, string_2)
.addOnCompleteListener(Auth1Activity.this, new OnCompleteListener<AuthResult>() {

@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
if (firebaseUser.isEmailVerified()) {
Intent intent = new Intent(Auth1Activity.this, HomeActivity.class);
startActivity(intent);
finish();
} else if (!firebaseUser.isEmailVerified()) {
Toast.makeText(Auth1Activity.this, "Sign Up Error Please Try Again.", Toast.LENGTH_SHORT).show();
}
} catch (NullPointerException ignored) {

}
}
});
}
}
});
}

@Override
public void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(authStateListener);
}

@Override
public void onStop() {
super.onStop();

if (authStateListener != null) {
firebaseAuth.removeAuthStateListener(authStateListener);
}
}
}

可能出了什么问题?

最佳答案

试试这个

    authStateListener = new FirebaseAuth.AuthStateListener() {

@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
firebaseUser = firebaseAuth.getCurrentUser();

if (firebaseUser != null) {

Intent intent = new Intent(Auth1Activity.this, HomeActivity.class);
startActivity(intent);
finish();

}

}

};

关于java - Firebase Auth 在单击按钮时未启动 Activity 但在关闭并重新打开应用程序时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51396376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com