0

There are ten activity java code, seven activity need to firebase data. so i added firebaseListener(addChildEventListener). Because data is updated in each activity, each activity need to each Listener. In my mind, it can work just one code...(because many people will say this is redundancy) service, thred..? what i have to use.

    mDatabase.child("Room-List").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                String key = dataSnapshot.getKey();
               if(key.equals(roomName)) {
                   room = dataSnapshot.getValue(Room.class);
                   gap = room.gap;
                   nextTime = room.nextTime;
                   gameMode = room.gameMode;
                   break;
               }
            }
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {            }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {            }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {            }
        @Override
        public void onCancelled(DatabaseError databaseError) {            }
    });


    //Member-List - nickName, online, state, position
    mDatabase.child("Room-List").child(roomName).child("Member-List").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            AllnickName = "";
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                Person person = dataSnapshot.getValue(Person.class);
                if(person.online) {
                    online++;
                    AllnickName += person.nickName;
                    break;
                }
            }
            TextView_person.setText(AllnickName);
            TextView_roomName.setText(roomName + online);
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {            }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {            }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {            }
        @Override
        public void onCancelled(DatabaseError databaseError) {            }
    });

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Lab Pa
  • 19
  • 3

1 Answers1

0

There is no need for a Service or Tread to do this. When your app start you can spin up a Singletons in Android. In the Singleton you can host the ChildEventListener code and it can be accessed by any Activity who need the ChildEventListener Data.

If your Activity closes and restarted the Singleton will not be affected by that.

What is the purpose of Singleton?
The purpose of the Singleton class is to control object creation, limiting the number of objects to only one. The singleton allows only one entry point to create the new instance of the class.

Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons are often useful where you have to control the resources, such as database connections or sockets.

If user or Android system closes your app the ChildEventListener and Singleton is also killed until user start your app again. If you need the ChildEventListener to be auto restarted in this case and so to speak "always be alive" you have to use a [Android Service][2]

Erik
  • 5,039
  • 10
  • 63
  • 119
  • Thanks! I understood that it would be easy to use. but, i forgot releasing Listener, so it makes problem.. But if i used Singleton, that's problem not occur.. – Lab Pa May 26 '19 at 15:56