I have problem with deleting the account for user. The deleting code is successful and it works. The only thing that doesnt work is after deleting the account the app should bring user to the loginpage but instead it bring the user to the mainActivity page which is weird why it behave like that? I have no idea what went wrong.
//update account
if (currentUser != null) {
userId = currentUser.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference("Customer").child(firebaseAuth.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Customer cust = dataSnapshot.getValue(Customer.class);
name.setText(cust.name);
address.setText(cust.home_address);
phone.setText(cust.telephone_number);
email.setText(cust.email);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.i("Test", name.getText().toString());
dataSnapshot.getRef().child("name").setValue(name.getText().toString());
dataSnapshot.getRef().child("home_address").setValue(address.getText().toString());
dataSnapshot.getRef().child("telephone_number").setValue(phone.getText().toString());
Toast.makeText(getApplicationContext(), "Successfully updated!", Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("User", databaseError.getMessage());
}
});
}
});
}
public void deleteAccount(View view) {
progBar = findViewById(R.id.progBar);
firebaseAuth = FirebaseAuth.getInstance();
AlertDialog.Builder dialog = new AlertDialog.Builder(CustProfileActivity.this);
dialog.setTitle("Are you sure ? ");
dialog.setMessage("Deleting this account will result in completely removing your " +
" account from the system and you won't be able to access the app.");
dialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
progBar.setVisibility(View.VISIBLE);
FirebaseDatabase.getInstance().getReference().child("Customer").child(firebaseAuth.getCurrentUser().getUid()).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
progBar.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Account Deleted", Toast.LENGTH_SHORT).show();
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(CustProfileActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
} else {
Toast.makeText(CustProfileActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
});
dialog.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = dialog.create();
alertDialog.show();
}