在firebase身份验证之后启动新活动

时间:2017-09-22 17:58:24

标签: java android firebase-authentication start-activity

这是我的代码...... 在此代码中使用intent来开始一个新活动... 注册用户电子邮件和密码后,新活动应该打开....我试着自己但是,它正在开始新的活动而没有注册。 我希望用户在注册后进入第二个活动。     `//定义视图对象     私人EditText editTextEmail;     private EditText editTextPassword;     私人按钮按钮;     private ProgressDialog progressDialog;

//defining firebaseauth object
private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_activity);

    //initializing firebase auth object
    firebaseAuth = FirebaseAuth.getInstance();

    //initializing views
    editTextEmail = (EditText) findViewById(R.id.editText3);
    editTextPassword = (EditText) findViewById(R.id.editText4);

    buttonSignup = (Button) findViewById(R.id.buttonSignup);

    progressDialog = new ProgressDialog(this);

    //attaching listener to button
    buttonSignup.setOnClickListener(this);
}

private void registerUser(){

    //getting email and password from edit texts
    String email = editTextEmail.getText().toString().trim();
    String password  = editTextPassword.getText().toString().trim();

    //checking if email and passwords are empty
    if(TextUtils.isEmpty(email)){
        Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
        return;
    }

    if(TextUtils.isEmpty(password)){
        Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
        return;
    }

    //if the email and password are not empty
    //displaying a progress dialog

    progressDialog.setMessage("Registering Please Wait...");
    progressDialog.show();

    //creating a new user
    firebaseAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    //checking if success
                    if(task.isSuccessful()){
                        //display some message here
                        Toast.makeText(LoginActivity.this,"Successfully registered",Toast.LENGTH_LONG).show();
                    }else{
                        //display some message here
                        Toast.makeText(LoginActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
                    }
                    progressDialog.dismiss();
                }
            });

   startActivity(new Intent(this,SecondActivity.class));

}

  @Override 

    public void onClick(View view) {

    //calling register method on click
    registerUser();
}
 }`

2 个答案:

答案 0 :(得分:0)

试试这个

  • task.isSuccessful传递intent
 firebaseAuth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if(task.isSuccessful()){
                   startActivity(new Intent(current_activity.this,next_activity.class));
                }else{
                }

            }
        });

乐意帮助您

答案 1 :(得分:0)

startActivity必须在task.isSuccessful()方法中调用。目前,您甚至在获得成功响应之前就会调用startActivity。