java - Firebase addValueEventListener keeps listening in other activity -


i have code in activity called "principalactivity.java"

btnsignin.setonclicklistener(new view.onclicklistener() {     @override     public void onclick(view view) {         final string placa = edtplaca.gettext().tostring().trim();         final string token = edttoken.gettext().tostring().trim();         if (textutils.isempty(placa)) {             toast.maketext(getapplicationcontext(), "ingrese una placa", toast.length_short).show();             return;         }         if (textutils.isempty(token)) {             toast.maketext(getapplicationcontext(), "ingrese el token de seguridad", toast.length_short).show();             return;         }         mapaintent = new intent(getapplicationcontext(),mapaactivity.class);         startactivity(mapaintent);         toast.maketext(getapplicationcontext(), "principal", toast.length_short).show();         myref.child("lineas").addvalueeventlistener(new valueeventlistener() {             @override             public void ondatachange(datasnapshot datasnapshot) {                 try{                     object valor = "";                     string clave = "";                     mapaintent = new intent(getbasecontext(),mapaactivity.class);                     (datasnapshot lineas: datasnapshot.getchildren()) {//navegar por lineas                         (datasnapshot placas: lineas.getchildren()){//navegar por placas                             if (placas.getkey().tostring().equals(placa.trim())) {                                 (datasnapshot llave : placas.getchildren()) {//navegar por campos de cada placa                                     valor = llave.getvalue();                                     clave = llave.getkey();                                     if (clave.equals("token")) {                                         if (valor.equals(token.trim())) {                                             toast.maketext(getapplicationcontext(), "placa y token correctos!!!", toast.length_short).show();                                             startactivityforresult(mapaintent,123);                                         } else {                                             toast.maketext(getapplicationcontext(), "token de seguridad incorrecto", toast.length_short).show();                                         }                                     }                                 }                             }                         }                         toast.maketext(getapplicationcontext(),"la placa no existe",toast.length_short).show();                      }                 }catch (exception ex){                     toast.maketext(getapplicationcontext(),"error leyendo",toast.length_short).show();                 }             }      @override     public void oncancelled(databaseerror databaseerror) {      } }); 

well, can see code part of listener of button called btnsigin. see try bloc, i'm trying start other activity called mapaactivity. intent inside addvalueeventlistener (a firebase listener when field change).

in mapaactivity class have this

 class mylocationlistener implements locationlistener {      mylocationlistener(){         ubicacion = new location("inicio");         ubicacion.setlatitude(0.0);         ubicacion.setlongitude(0.0);     }      @override     public void onlocationchanged(location location) {         if (muestreando){             ubicacion = location;             txtlat.settext("latitud: "+ location.getlatitude());             txtlon.settext("longitud: "+ location.getlongitude());             myref.child("lineas").child("1").child("abc-123").child("latitud").setvalue(location.getlatitude());             myref.child("lineas").child("1").child("abc-123").child("longitd").setvalue(location.getlongitude());             setresult(1);         }     }      @override     public void onstatuschanged(string s, int i, bundle bundle) {      }      @override     public void onproviderenabled(string s) {      } 

as can see listener modify parameters user location in firebase.

the problem

the first class works , sends me second activity (mapaactivity).

but, when second activity modifies firebase database reason (tha can't undestand), first activity listen change n firebase , creates again de second activity , how that. happens many times location change (it refresh firebase , first activity listen , trhougt again seond activity).

anybody knows why ? , how can solve. thank you.

addvalueeventlistener not event can triggered click of button or something. listening firebase changes , updates.

addvalueeventlistener callback interface set when there change in firebase database.

once jump activity stop listening eventlistener

you have method removeeventlistener can used remove lsiteners avoid getting callback firebase.

dbref.removeeventlistener(yourlistener); 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -