为什么这个吐司不应该出现?

时间:2020-02-28 20:02:21

标签: java android class methods toast

你好

我有一种方法可以验证数据库中是否已经存在电话号码或电子邮件,如果这样,用户将得到一个吐司,说其已经注册,如果没有,将返回到SignUp活动,然后继续执行代码,

public class VerficatePhone extends AppCompatActivity {
private String mVerficationId,phoneNumber,sentCode;
private User user;
private EditText phoneCodeEditText;
private PhoneAuthProvider.ForceResendingToken mResend;
private Button activate;
private TextView backToSignupText;
private ProgressBar pBar;
private FirebaseAuth mAuth;
private DatabaseReference reff;
private boolean executed =false;



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

    phoneCodeEditText=findViewById(R.id.verificationEditText);
    activate=findViewById(R.id.activate);
    backToSignupText=findViewById(R.id.backToSignupText);
    pBar=findViewById(R.id.progressBar);

    mAuth=FirebaseAuth.getInstance();
    reff= FirebaseDatabase.getInstance().getReference().child("User");


    //getting the user that has been sent from the sign up Activity and the phone number
    if(getIntent().getSerializableExtra("userFromSignUp")!=null)
    {
        user=(User)getIntent().getSerializableExtra("userFromSignUp");
        phoneNumber=user.getPhoneNumber();

    }
    else
    {
        phoneNumber="000";
        Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show();
    }

    if(!executed)
    {
        checkPhoneNumIfExists(user);
        executed =true;
    }

    sendCode(phoneNumber);
}

我的checkPhoneNumIfExists方法是这样

 private void checkPhoneNumIfExists (final User currentUser)
{
    Log.i("checkPhoneNumIfExists method","processing");
     FirebaseDatabase fDb=FirebaseDatabase.getInstance();
        DatabaseReference dbRef=fDb.getReference();
        dbRef.child("User").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Iterable<DataSnapshot> children=dataSnapshot.getChildren();
                for(DataSnapshot child:children)
                {
                    if(currentUser!=null && child!=null)
                    {
                        User user= child.getValue(User.class);
                        if(user!=null && !user.getPhoneNumber().isEmpty())
                        {
                            if(currentUser.getPhoneNumber().equals(user.getPhoneNumber()) || currentUser.geteMail().equals(user.geteMail()))
                            {
                                Toast.makeText(VerficatePhone.this,"already registred",Toast.LENGTH_SHORT).show();
                                Intent i =new Intent(VerficatePhone.this,signUpActivity.class);
                                i.putExtra("userFromVerificate",currentUser);
                                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(i);
                                finish();



                            }
                        }

                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    executed =true;

}

所以这就是问题所在,当用户未注册时,继续执行代码并在onCreate方法中执行sendCode方法,这将打开一个新活动,但是每当它从sendCode方法中打开新活动时,checkPhoneNumIfExists方法的敬酒它出现了。

那为什么会发生?

1 个答案:

答案 0 :(得分:0)

onDataChange()是一个异步回调。依赖于它的任何数据或函数都应在回调内部进行访问或触发。

由于看起来您不想每次都运行数据库检查,所以我建议您查看SharedPreferences以在您离开并返回活动时持久保存变量。您可以在executed执行时存储onDataChange()的值,并在if语句之前在onCreate()中检索值。

@Override
protected void onCreate(Bundle savedInstanceState){
    // ...

    // If you're using SharedPreferences, retrieve the value here

    if(!executed){
        checkDb(user)
    }else{
        // I'm assuming you only want to send the code when executed is true
        // which means that, for this approach, you should call sendCode()
        // again inside of your onDataChange()
        sendCode(phoneNum);
    }
}

private void checkDb(final User currentUser){
    // ...

    myRef.addValueEventListener(new ValueEventListener(){
        @Override
        public void onDataChange(DataSnapshot dataSnapshot){
            for(DataSnapshot snap: dataSnapshot.getChildren()){
                // do something with the data
                // ...
            }

            // This is where you can set the value of a variable 
            // or execute a function that relies upon the data of the callback. 

            // If you're using SharedPreferences, you would store the value here.

            // You may want to call sendCode() again here
        }
    });
}

您可以通过访问https://developer.android.com/training/data-storage/shared-preferences

了解有关SharedPreferences的信息。