具有Firebase怪异行为的SignIn界面

艾哈迈德·谢里夫(Ahmed El Sherif)
//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活动状态,否则将其移至活动状态。MainActivity

//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();
                    }
                });
            }
        });
    }
}

上面的代码是用户首次登录时将其重定向到的活动,因此,基本上,他添加了有关自己的更多数据以发送到firebase

我在代码中添加了一些Logcat,在运行代码后我注意到了奇怪的行为,下面我将发布Logcat的顺序,如Android Studio所示

//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活动中的某些原因,用户被重定向回该SignIn活动,因此它会检查这是否是用户首次登录AGAIN!,if条件将返回true,因此用户会转到MainActivity,我不确定这正在发生

艾哈迈德·谢里夫(Ahmed El Sherif)

终于我解决了,我只是不了解ValueEventListenersfirebase的其他用途

因此,当我打电话给checkIfDriverExists()时,我会通过allDrivers节点使用他的电子邮件地址搜索驱动程序,而我过去经常addValueEventListener在其中寻找所有孩子。

这是我的错误,因为在CompleteProfile活动内部我将数据对象传递给另一个更新allDrivers节点的函数,因此addValueEventListener静态对象仍在侦听数据更改,因此返回trueDriverExistance回调,然后转到SignIn活动,然后从那里转到MainActivityas。听者发现驱动程序Exists

我通过更改addValueEventListeneraddListenerForSingleValueEvent内部checkDriverIfExist函数解决了

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章