Firebase Auth不能在单击按钮时启动“活动”,但在关闭并重新打开应用程序后可以运行

时间:2018-07-18 07:46:36

标签: java android firebase firebase-authentication

Firebase身份验证不是在按钮单击时从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);
        }
    }
}

出什么问题了?

1 个答案:

答案 0 :(得分:1)

尝试这种方式

    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();

        }

    }

};