使用Firebase Realtime删除帐户

时间:2020-01-20 01:56:12

标签: java android firebase-realtime-database delete-operator

我在删除用户帐户时遇到问题。删除代码成功并且有效。唯一不起作用的是在删除帐户后,应用程序应将用户带到登录页面,但是它将用户带到mainActivity页面,这很奇怪为什么会这样操作?我不知道出了什么问题。

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

    }


enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在此行替换以下代码:

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

与此:

  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)
                    startActivity(intent)
                         finishAffinity()

                    }
相关问题