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

问题描述 投票:0回答:1

Hello

我有一种方法可以验证数据库中是否已经存在电话号码或电子邮件,如果是这样,则用户将吐司说其已注册,如果没有,将返回到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方法的敬酒出现。

为什么会这样?

java android class methods toast
1个回答
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的信息>
© www.soinside.com 2019 - 2024. All rights reserved.