2

I'm creating quiz app. The questions in the app are based on some values that are constantly changing. So I'm using a addSnapshotListener() to get every change that is made in the database. The docs stays that I should remove the listener, which is fine but the problem is that in my app, the orientation is changing very often. This means that I attach and remove the listener too many times. Is this a bad approach? How to solve this?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Oleg Caralanski
  • 330
  • 2
  • 10

1 Answers1

2

Is this a bad approach?

No, it's not! You should definitely remove the listener once it's not needed anymore. I assume you are adding the listener in your onStart() method and removing it in your onStop() method of your activity, right? If so, please note that this is the normal behaviour, since both methods are apart of the life-cycle of an activity and are called every time the orientation is changed. So everytime the reorientation takes place, the activity is destroyed and recreated again. Please see more infos:

If you want a more elegant way to remove the listener, you should see the last part of my answer from this post. So you can pass the activity as the first argument in the addSnapshotListener() method and the listener will be removed automatically for you.

Edit:

According to your comment, you're right. Even if you are using that solution, the number of attaching and removing the listener would be the same. In this case, I have a solution that can reduce that number.

private boolean pending = false;
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        //Remove listener
        pending = false;
    }
};

@Override
protected void onStart() {
    super.onStart();
    if (pending) {
        handler.removeCallbacks(runnable);
    } else {
        //Attach listener
    }
    pending = false;
}

@Override
protected void onStop() {
    super.onStop();
    handler.postDelayed(runnable, 3000);
    pending = true;
}

Which basically means that the Handler will schedule the removal of your listener by using a Runnable callback that will actualy perform the removal after three seconds after the call to onStop(). I've set the delay to three seconds for the orientation change but in real world situation it's normally much faster even on old phones.

So if the orientation is faster than those three seconds, we simply remove the callbacks and allow the listener to keep listening. This obviously means that you reduce the number of removing the listener.

This will be also very useful because there will be not a second round trip to and from the Firestore backend to pull down the data even if the results did not change.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I think you did not understand my question. Even if I use that solution, the listener is attached and removed by the exact same number of times as before. – Oleg Caralanski Feb 08 '19 at 14:28