Home > Blockchain >  Should I add snapshotListener inside onResume or onStart?
Should I add snapshotListener inside onResume or onStart?

Time:09-21

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?

CodePudding user response:

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.

CodePudding user response:

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.

  • Related