Issue
I am using Firebase Auth (registering a new user) , it worked perfectly on the Main Thread but when i moved it to the background , Authentication always failed . Here is the the method for creating a new user :
public void createAccount(String email, String password) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(registerActivity.this, "bad",
Toast.LENGTH_SHORT).show();
sr = false;
} else {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
EditText etname = (EditText) findViewById(R.id.etName);
EditText etlname = (EditText) findViewById(R.id.etLName);
String namez = etname.getText().toString();
String lnamez = etlname.getText().toString();
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
Toast.makeText(registerActivity.this, "good", Toast.LENGTH_SHORT).show();
String UID = user.getUid();
mDatabase.child("users").child(UID + "_lname").setValue(lnamez);
mDatabase.child("users").child(UID + "_name").setValue(namez);
sr = true;
}
}
// ...
}
});
}
and here is the AsyncTask :
private class createInBack extends AsyncTask<Void, Void, Void> {
//Executes the 'createAccount' method in background .
EditText etemail = (EditText) findViewById(R.id.etEmail);
EditText etpassword = (EditText) findViewById(R.id.etPassword);
String email = etemail.getText().toString();
String password = etpassword.getText().toString();
@Override
protected void onPreExecute() {
super.onPreExecute();
spinner.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... voids) {
createAccount(email, password);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(sr){
Toast.makeText(registerActivity.this, "we good here", Toast.LENGTH_SHORT).show();
startActivity(new Intent(registerActivity.this, MainActivity.class));
}
spinner.setVisibility(View.GONE);
}
}
so what is the problem ?
Solution
Problem Solved , I found in the XML layout that i forgot to remove the "android:onClick" from the button.
Answered By - Anas Tawtaw
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.