1

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

    }


enter image description here

last gg
  • 87
  • 1
  • 9
  • Looks related: https://stackoverflow.com/questions/42571618/how-to-make-a-user-sign-out-in-firebase – user2740650 Jan 20 '20 at 02:01
  • I have looked into the discussion, but can you enlighten me what's related? – last gg Jan 20 '20 at 02:04
  • Sorry my bad. I thought at first they were doing the same thing. – user2740650 Jan 20 '20 at 02:12
  • You can check **[this](https://stackoverflow.com/questions/50885891/one-time-login-in-app-firebaseauth)** out. – Alex Mamo Jan 20 '20 at 11:00
  • thank you, but i don't really quite sure what i have to do with this. – last gg Jan 20 '20 at 11:14
  • You're trying to fetch data (name) from a null object. In CustProfileActivity line 115. Post the code if you want us to take a look – Itay Feldman Jan 20 '20 at 11:18
  • The deleting code is working fine it just that it didn't want to go to login page which is weird because when I see my database the data has been deleted and instead of going to loginpage it goes to mainactivity page. – last gg Jan 20 '20 at 11:25
  • @ItayFeldman and of course it will return null because the data has been deleted inside the database. That is the reason why it can't read. – last gg Jan 20 '20 at 11:27
  • Ok so the crash is because it navigates to the wrong place. Where in your app do you start the MainActivity? in the Login activity if you have a current user from firebase? – Itay Feldman Jan 20 '20 at 11:30
  • my apps always start with MainActivity. – last gg Jan 20 '20 at 11:34

1 Answers1

0

Can you replace this line this code :

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

With This :

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

                    }
haresh
  • 1,424
  • 2
  • 12
  • 18