0

I'm looking for a way to listen to a particular location in my database in every activity using one ChildEventListener.

I have a shopping app, and I need to listen to the user's order status through the app to show alerts and save data locally.

I had 2 ideas in mind:

  1. I thought about attaching and detaching the listener in OnResume()
    and OnPause() of every Activity, but the code will be duplicated in every Activity since it does the same job.
  2. I thought about attaching one listener to the MainActivity, then send broadcasts to other activities, but this failed due to MainActivity being destroyed

--

What is the best practice for listening to one node and keep app synchronized?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Khaled
  • 542
  • 1
  • 5
  • 10

3 Answers3

1

There is no problem in Firebase when using multiple listeners, even if it's the same listener. As long as you remove the listener according to the life-cycle of your activities, Firebase will handle almost perfectly the listeners.

Your class should look like this:

public class HelperClass extends Application {
    private DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

    public static verifyStatus() {
        databaseReference.addChildEventListener() /* ... */
    }
}

You need to remove the listener accordingly to the life-cycle of your activity. If you have added the listener in onStart you have to remove it in onStop. If you have added the listener in onResume you have to remove it in onPause. If you have added the listener in onCreate you have to remove it in onDestroy. But remember onDestroy is not always called.

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • You're right, but I'm aiming to centralize the work, so it's easy to modify the code later. – Khaled Aug 04 '17 at 13:28
  • 2
    In this case you can create a `HelperClass` in which you can create a method that can be reused in every activity you need. – Alex Mamo Aug 04 '17 at 13:29
  • That was simple... Sounds like an idea, but wouldn't there be a delay if I'm creating listeners in each activity? – Khaled Aug 04 '17 at 13:32
  • Now, there will be now delays because each listener will be treated separately. Remember, Firebase it's a Real-time database. – Alex Mamo Aug 04 '17 at 13:34
  • Thank you, that's what I needed to hear, any recommendation regarding life-cycle? will `onPause()` and `onResume()` do the job perfectly? – Khaled Aug 04 '17 at 13:36
  • No. You need to remove the listener accordingly to the life-cycle of your activity. If you have added the listener in `onStart` you have to remove it in `onStop`. If you have added the listener in `onResume` you have to remove it in `onPause`. If you have added the listener in `onCreate` you have to remove it in `onDestroy`. But remember `onDestroy` is not always called. Best solution are `onPause()` and `onResume()`. I also edited my answer. – Alex Mamo Aug 04 '17 at 13:45
  • Sorry, I'm not sure how to use this `HelperClass`, do I call `verifyStatus` in `onResume()` of every activity? – Khaled Aug 04 '17 at 14:01
  • You call verifyStatus() wherever you need in every activity. – Alex Mamo Aug 04 '17 at 14:04
  • And where do I remove the listener? – Khaled Aug 04 '17 at 14:07
  • You need to create another method which removes the listener,. You can call it `public static void removeListener() {...}`. And you need to call it acordingly to your activity life-cycle. Please see the updated answer. – Alex Mamo Aug 04 '17 at 14:10
0

Try this way create a class and inside class create the method that you want like that:

public class firebaseutils {

public static void check(){

    }

}

And call this class where you want, like that:

firebaseutils.check();
Bruno Ferreira
  • 1,561
  • 1
  • 10
  • 17
0

recently I solved a same problem. So, make a handler class of the ChildEventListener and its has a single instance like FirebaseDatabase, FirebaseAuth and others Firebase services. That class is the same this snippet:

public class ShopHandler implements ChildEventListener {

    private static ShopHandler current = null;
    private DatabaseReference databaseReference;

    private ShopHandler() {}

    public synchronized static void attach() {
        if (current = null) {
            current = new ShopHandler();
            databaseReference = FirebaseDatabase.getInstance().getReference("data/yournode");
            databaseReference.addChildEventListener(this);
        }
    }

    public syncrhonized static void detach() {
         // remove listeners and set current as null
    }
    // implements all methods about ChildEventListener

}
Marcus
  • 773
  • 8
  • 11