有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何防止注册后重定向到其他活动?

此代码在public void onSuccess(void aVoid)之前工作正常,但在执行之后,活动立即重定向到上一个活动,而不是停留在其上,然后出现toast消息

我希望它在注册用户后保留在活动中

mFireBaseAuth=FirebaseAuth。getInstance(); mfirebaseDatabase=FirebaseDatabase。getInstance()

    mFireBaseAuth.createUserWithEmailAndPassword(e_mail, password).addOnCompleteListener(StudentSignUp.this, new OnCompleteListener<AuthResult>()
    {

        @Override
        public void onComplete(@NonNull Task<AuthResult> task)
        {

            if(!task.isSuccessful())
            {

                Toast.makeText(StudentSignUp.this, "Please check your network or try again using different E-mail.", Toast.LENGTH_SHORT).show();
                //loadingBar.dismiss();

            }

            else
            {

                StudentDetails studentCredentials = new StudentDetails( studentId, name, e_mail, password );
                String uId = task.getResult().getUser().getUid();
                mfirebaseDatabase.getReference().child("Student").child(uId).setValue(studentCredentials).addOnSuccessListener(new OnSuccessListener<Void>()
                {

                    @Override
                    public void onSuccess(Void aVoid)
                    {

                        Toast.makeText(StudentSignUp.this, "Your Student is Registered & account has been created.", Toast.LENGTH_SHORT).show();
                        //loadingBar.dismiss();

                    }

                });

            }

        }

    });

}

共 (1) 个答案

  1. # 1 楼答案

    为什么需要在onCompleteListener中传递上下文

    代码应该是这样的:

      mFireBaseAuth.createUserWithEmailAndPassword(e_mail, password).addOnCompleteListener(new OnCompleteListener<AuthResult>()
    {
    
        @Override
        public void onComplete(@NonNull Task<AuthResult> task)
        {
    
            if(!task.isSuccessful())
            {
    
                Toast.makeText(StudentSignUp.this, "Please check your network or try again using different E-mail.", Toast.LENGTH_SHORT).show();
                //loadingBar.dismiss();
    
            }
    
            else
            {
    
                StudentDetails studentCredentials = new StudentDetails( studentId, name, e_mail, password );
                String uId = task.getResult().getUser().getUid();
                mfirebaseDatabase.getReference().child("Student").child(uId).setValue(studentCredentials).addOnSuccessListener(new OnSuccessListener<Void>()
                {
    
                    @Override
                    public void onSuccess(Void aVoid)
                    {
    
                        Toast.makeText(StudentSignUp.this, "Your Student is Registered & account has been created.", Toast.LENGTH_SHORT).show();
                        //loadingBar.dismiss();
    
                    }
    
                });
    
            }
    
        }
    
    });
    

    与使用onCompleteListener和if(!task.issusccessful())不同,您还可以使用onSuccessListener,有时我们会忘记检查(!task.issusccessful()),所以最好使用onSuccessListener😀

    如果有不清楚的地方,请随时询问如果这个答案对你有帮助,请将其标记为正确答案,并且在将来这个答案也可以帮助其他需要帮助的人