为什么我必须点击两次?

时间:2016-11-07 22:37:40

标签: android firebase firebase-realtime-database firebase-authentication

我在LoginActivity

中有这段代码
public class LoginActivity extends AppCompatActivity{

    private final String TAG = "LoginActivity";
    private FirebaseAuth fAuth;
    private FirebaseAuth.AuthStateListener fAuthListener;
    private FirebaseUser fUser;
    private EditText eEmail, ePsw;
    private ProgressDialog progressDialog;
    private Intent i;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        i = new Intent(LoginActivity.this, UserMenu.class);
        eEmail = (EditText) findViewById(R.id.input_email);
        ePsw = (EditText) findViewById(R.id.input_password);
        Button b = (Button) findViewById(R.id.btn_login);
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        fAuth = FirebaseAuth.getInstance();
        fAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                fUser = firebaseAuth.getInstance().getCurrentUser();
                if(fUser != null){
                    userExist();
                }else{
                    Log.d(TAG, "Log out");
                }
            }
        };
        progressDialog = new ProgressDialog(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        fAuth.addAuthStateListener(fAuthListener);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        fAuth.addAuthStateListener(fAuthListener);
    }

    private void userExist(){
        DatabaseReference db = FirebaseDatabase.getInstance().getReference();
        DatabaseReference figli = db.child("users").child(fUser.getUid());
        figli.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.getValue() == null){
                    startActivity(i);
                }else {
                    startActivity(new Intent(LoginActivity.this, PanelUser.class));
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (fAuthListener != null) {
            fAuth.removeAuthStateListener(fAuthListener);
        }
    }

    public void signUp(View v){
        startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
    }

    private void login(){
        String email = eEmail.getText().toString();
        String psw = ePsw.getText().toString();
        if(TextUtils.isEmpty(email) || TextUtils.isEmpty(psw)){
            Toast.makeText(getApplicationContext(), "Email or password is empty", Toast.LENGTH_LONG).show();
            return;
        }
        progressDialog.setMessage("Login...");
        progressDialog.show();
        fAuth.signInWithEmailAndPassword(email, psw).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    progressDialog.dismiss();
                }else{
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(), "Email or password wrong", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

如果我打开应用程序并且已经有一个打开的会话,那么它可以正常工作并打开活动ProfileUser

相反,如果用户需要进行身份验证,则必须在登录按钮上单击两次才能打开活动ProfileUser

我该如何解决?

我必须检查用户是否已在数据库Firebase中输入了一些信息。如果您输入了姓名,姓氏和城市,那么您应该打开活动ProfileUser,否则他必须访问活动用户菜单

1 个答案:

答案 0 :(得分:1)

看起来您在用户通过身份验证后已成功登录,但只是解雇了progressDialog

之后调用userExist(),以便能够执行必要的实施并启动活动(PanelUserUserMenu