I have an activity name "CoachAlumnos" where it display the data on a recyclerView of persons from the firebase database, i use the FirebaseDatabase.getInstance ().getreference ().child("Usuario").addValueEventListener{..} It works fine. I have a button on this activity that when i press it it takes me to another activity name "AgregarAlumnos" it adds a new person, it works fine, but when it finish i should pass to a third activity, but while it is uploading the data to the firebase it automatically returns to the first activity, i think this is because the event listener of the first activity. And i dont know how to stop it, i already use the removeEventListener on the first activity but it doesnt work.

I use this:

@Override
protected void onStart (){
    super.onStart();

    obtenerdatoscoach ();
    obtenerdatosdeportistas();
}



private void obtenerdatosdeportistas() {
    basedatos.getReference ().child ("Usuario-Alumno").addValueEventListener(listenerdeportisa = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot objeto : dataSnapshot.getChildren()) {
                Usuario deportista = objeto.getValue(Usuario.class);
                assert deportista != null;
                if (deportista.obtenerCoach().equals (getIntent ().getStringExtra("email"))) {
                    alumnoslista.add(new AlumnoCard(
                            deportista.obtenerImageUrl(),
                            deportista.obtenerNombres() + " " + deportista.obtenerApellidos(),
                            deportista.obtenerTelefono(),
                            deportista.obtenerEmail(),
                            deportista.obtenerCoach(),
                            deportista.obtenerIdUsuario()));
                    adapter.notifyDataSetChanged();
                }
            }
            if (alumnoslista.size() == 0) {
                AlertDialog.Builder builder = new AlertDialog
                        .Builder(CoachAlumnos.this, R.style.DialogBasicCustomStyle);
                builder.setIcon(R.drawable.icono_agregar_deportista);
                builder.setTitle("AGREGUE");
                builder.setMessage("NO CUENTA AUN CON DEPORTISTAS, AGREGUE A SU PRIMER DEPORTISTA")
                        .setPositiveButton("AGREGAR", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent nuevo = new Intent(CoachAlumnos.this, AgregarNuevo.class);
                                nuevo.putExtra("email", getIntent().getStringExtra("email"));
                                startActivity(nuevo);
                            }
                        }).setNegativeButton("AHORA NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        progressBar.setVisibility(View.GONE);
                        dialog.dismiss();
                    }
                }).setCancelable(false)
                        .show();
            }
            progressBar.setVisibility(View.GONE);
        //basedatos.getReference ().child ("Usuario-Coach").removeEventListener(listenerdeportisa); //I also try this
        }

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

        }
    });
}

private void obtenerdatoscoach (){
    basedatos.getReference ().child ("Usuario-Alumno").addValueEventListener(listenercoach = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot objeto : dataSnapshot.getChildren ()){
                Usuario coach = objeto.getValue(Usuario.class);
                assert coach != null;
                if (coach.obtenerEmail().equals (getIntent ().getStringExtra("email"))){
                    coachid = coach.obtenerIdUsuario();
                    nombrecoach = coach.obtenerNombres() + " " + coach.obtenerApellidos();
                    foto_coach = coach.obtenerImageUrl();
                    Picasso.with (CoachAlumnos.this).load(foto_coach).into(perfilButton);
                    progressBar.setVisibility(View.GONE);
                    break;
                }
            }
        //basedatos.getReference ().child ("Usuario-Alumno").removeEventListener(listenerdeportisa); //I also try this
        }

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

        }
    });
}



@Override
protected void onDestroy (){
    super.onDestroy();
    if (listenerdeportisa != null)
        basedatos.getReference ().child ("Usuario-Alumno").removeEventListener(listenerdeportisa);
    if (listenercoach != null)
        basedatos.getReference ().child ("Usuario-Coach").removeEventListener(listenercoach);
}

0 Answers