skip to Main Content

According to Firestore documentation, we can simply add and remove snapshotListener automatically just by passing the instance of Activity to the snapshotListener. But the snapshotListener can only be removed onStop. onStop() is not guaranteed to be called all the time. Should I add/remove my snapshotListener onResume/onPause instead?

2

Answers


  1. If you’re not satisfied with when the automatic cancelation of snapshot listeners happen when you pass an Activity when registering them, you can instead manage the listener lifecycle yourself. There is nothing wrong with this, and is in fact what I usually do.

    There is no singular correct answer here. It all depends on your preferences and the requirements of your app.

    Login or Signup to reply.
  2. onStop is not guaranteed to be called because the system may simply terminate the process when it’s not in the foreground and resources are needed for other apps. It’s not the Android is somehow buggy – it’s just a result of the way that it might manage your application’s process along with other processes that need user attention.

    In the case that your application process is terminated, your database listener will be definitely gone, and you will not need to remove it. Managing the listener manually is of no real advantage in this case. It’s OK to trust the onStart/onStop callbacks here because they accurately indicate when the activity is visible to the user. If onStop is not called, you can be sure that the process is simply dead and will no longer consume resources.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search