Can´t finish Activity

2020-05-04 10:54发布

问题:

I have tried className.this.finish and finishActivity() to invoke the finishing of my activity and I have initialised the activity with the following methods - startActivityForResult() or finishAndRemoveTask()

The propose of this activity is to register a device this is done when under Devices/(MAC address of the device)/Users/ it save in fire base up to 4 different notification tokens of 4 users different. So what I'm trying to do but it falling is to check if the number 1 have info, check 2 if have info, check 3 and if this have info, check the 4 if all have info the app show a message saying "you have reached the maximum number of users".But if one number available it suppose to save in that number and only in that number the notification token.

What it is happening is that when I click the button the loop never end. it send the same notification token for the four users show the mensage "you have reached the maximum number of users" and then it come back to the activity before this one but it still executing the registration code. I know this because if I deleted an user in firebase it immediately resend the info and you can see the message "you have reached the maximum number of users" again

this is the code of the activity that send the info:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_cerca);

    fAuth = FirebaseAuth.getInstance();
    entr = findViewById(R.id.AddButton);
    mDatabase = FirebaseDatabase.getInstance().getReference();
    sDatabase = FirebaseDatabase.getInstance().getReference();
    aDatabase = FirebaseDatabase.getInstance().getReference();
    MAC =findViewById(R.id.macCerca);
    NOM = findViewById(R.id.momCerca);
    configured = false;
    getUserProfile();
    getNotificationId();

    entr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MacCerca = MAC.getText().toString().toUpperCase();
            NombreCerca = NOM.getText().toString();
            //mDatabase.child("Devices").child(MacCerca).child("Id").setValue("AD:23");
            mDatabase.child("Devices").child(MacCerca).child("Users").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    String user1 = (String) dataSnapshot.child("1").getValue();
                    String user2 = (String) dataSnapshot.child("2").getValue();
                    String user3 = (String) dataSnapshot.child("3").getValue();
                    String user4 = (String) dataSnapshot.child("4").getValue();
                    if(user1 == null){
                        aDatabase.child("Devices").child(MacCerca).child("Users").child("1").setValue(token);
                        aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
                        aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
                        AddCerca.this.finish();
                    }else if(user2 ==null){
                        aDatabase.child("Devices").child(MacCerca).child("Users").child("2").setValue(token);
                        aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
                        aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
                        AddCerca.this.finish();
                    }else if(user3 ==null){
                        aDatabase.child("Devices").child(MacCerca).child("Users").child("3").setValue(token);
                        aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
                        aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
                        AddCerca.this.finish();
                    }else if(user4 ==null){
                        aDatabase.child("Devices").child(MacCerca).child("Users").child("4").setValue(token);
                        aDatabase.child("Devices").child(MacCerca).child("NombreCerca").setValue(NombreCerca);
                        aDatabase.child("Users").child(mail).child("Device").setValue(MacCerca);
                        AddCerca.this.finish();
                    }else{
                        Toast.makeText(AddCerca.this, "Límite de usuarios registrados exedidos", Toast.LENGTH_SHORT).show();
                        AddCerca.this.finish();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    });
}

and I create this activity like this:

startActivity(new Intent(CercaElectrica.this, AddCerca.class));

Firebase look like this:

When it suposed to be only in the user 1, or if user 1 have some data it have to update user 2 and the same with 3 and 4

回答1:

I found the error. insted of:

mDatabase.child("Devices").child(MacCerca).child("Users").addValueEventListener(new ValueEventListener() {

change for:

mDatabase.child("Devices").child(MacCerca).child("Users").addListenerForSingleValueEvent(new ValueEventListener() {