SignIn中的接口与Firebase奇怪的行为

时间:2017-01-29 04:59:39

标签: android firebase interface callback firebase-authentication

//Login class
public class SignIn extends AppCompatActivity {

    private EditText mEmail;
    private EditText mPassword;
    private Button mButton;
    private FirebaseAuth mAuth;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Please Wait");
        mProgressDialog.setCancelable(false);
        setContentView(R.layout.activity_sign_in);
        mAuth = FirebaseAuth.getInstance();
        mEmail = (EditText) findViewById(R.id.email_login);
        mPassword = (EditText) findViewById(R.id.password_login);
        mButton = (Button) findViewById(R.id.login_btn);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mProgressDialog.show();
                final String email,password;
                email = mEmail.getText().toString();
                password = mPassword.getText().toString();
                mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful())
                        {
                            FirebaseHandler.checkIfDriverExist(email, new DriverExistance() {
                                @Override
                                public void onSearchComplete(boolean isFound) {
                                    if(isFound)
                                    {
                                        Toast.makeText(SignIn.this,"Logged In",Toast.LENGTH_SHORT).show();
                                        Log.v("debugMood","Login is found");
                                        startActivity(new Intent(SignIn.this,MainActivity.class));
                                        SignIn.this.finish();
                                    }
                                    else
                                    {
                                        Toast.makeText(SignIn.this,"Complete Your Profile",Toast.LENGTH_LONG).show();
                                        Log.v("debugMood","Login is not found");
                                        startActivity(new Intent(SignIn.this,CompleteProfile.class));
                                        SignIn.this.finish();
                                    }
                                }
                            });
                        }
                        else
                        {
                            Toast.makeText(SignIn.this,"Failed to Login",Toast.LENGTH_SHORT).show();
                        }
                        mProgressDialog.dismiss();
                    }
                });
            }
        });
    }
}

在上面的代码中,checkIfDriverExist检查用户是否是第一次登录,如果是第一次将其移至CompleteProfile活动,则将其移至{{1} }}

MainActivity

上面的代码是用户第一次登录时被重定向到的活动,所以基本上他添加了更多关于他自己的数据以发送到firebase

我已经在代码中添加了一些Logcats,我注意到在运行代码后出现了奇怪的行为,下面我会发布Android Studio所示的logcats顺序

//Complete profile
public class CompleteProfile extends AppCompatActivity {

    private EditText mName;
    private EditText mPassword;
    private EditText mPlateChars;
    private EditText mPlateNums;
    private EditText mPhoneNum;
    private Button mButton;
    private Driver mDriver;
    private FirebaseDatabase officeDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complete_profile);
        Log.v("debugMood","Complete profile bro");

        mDriver = new Driver();
        mName = (EditText) findViewById(R.id.driver_name_complete_profile);
        mPassword = (EditText) findViewById(R.id.password_complete_profile);
        mPlateChars = (EditText) findViewById(R.id.plate_chars_complete_profile);
        mPlateNums = (EditText) findViewById(R.id.plate_num_complete_profile);
        mPhoneNum = (EditText) findViewById(R.id.driver_phone_number_complete_profile);
        mButton = (Button) findViewById(R.id.complete_profile_btn);


        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDriver.setDriverEmail(FirebaseAuth.getInstance().getCurrentUser().getEmail());
                mDriver.setDriverPassword(mPassword.getText().toString());
                mDriver.setDriverName(mName.getText().toString());
                mDriver.setPlateChars(mPlateChars.getText().toString());
                mDriver.setPlateNums(mPlateNums.getText().toString());
                Log.v("debugMood","Complete profile before database handler");
                FirebaseHandler.completeDriverProfile(mDriver, FirebaseDatabase.getInstance(OfficeApp.officeApp(CompleteProfile.this))
                        ,new com.example.android.er123ambulance.callbacks.CompleteProfile() {
                    @Override
                    public void onProfileComplete() {
                        Log.v("debugMood","Complete profile after database handler");
                        Toast.makeText(CompleteProfile.this,"Profile Updated",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}

由于//Logs order 01-29 06:33:09.775 6341-6341/com.example.android.er123ambulance V/debugMood: Login is not found 01-29 06:33:10.635 6341-6341/com.example.android.er123ambulance V/debugMood: Complete profile bro 01-29 06:33:49.445 6341-6341/com.example.android.er123ambulance V/debugMood: Complete profile before database handler 01-29 06:33:49.675 6341-6341/com.example.android.er123ambulance V/debugMood: Login is found 01-29 06:33:56.775 6341-6341/com.example.android.er123ambulance V/debugMood: Complete profile after database handler 01-29 06:37:50.775 6341-6341/com.example.android.er123ambulance V/debugMood: Login is found 活动中的某些原因,用户被重定向回CompleteProfile活动,因此它会检查用户是否第一次登录AGAIN!,if条件将返回true,所以用户移动到SignIn,我不知道为什么会发生这种情况

1 个答案:

答案 0 :(得分:1)

最后我解决了它,我只是不明白使用不同的ValueEventListeners火焰基地..

因此,当我致电checkIfDriverExists()时,我通过allDrivers节点使用他的电子邮件地址搜索驱动程序,并使用addValueEventListener查找内部的所有子项。

这是我的错误,因为在CompleteProfile活动内部我将数据对象传递给另一个更新allDrivers节点的函数,因此addValueEventListener仍会侦听数据更改,因此返回{ {1}}转到true回调,因此转到DriverExistance活动,然后转到SignIn,因为听众发现了驱动程序MainActivity

我通过将Exists更改为addValueEventListener函数内的addListenerForSingleValueEvent来解决此问题