androidfirebasedatabase-connectionfirebase-realtime-databaseactivity-lifecycle

Close Firebase database connection when leaving Android app


I made an Android app which receives realtime data from Firebase database. In my main Activity the user has to log in with e-mail and password and by success it opens a new Activity, which receives data.

It seems to be, that when the user leaves my app, the Firebase connection is still established. That is not good for the battery.

How is the way to manage the connection for closing and reopen the app? I think I need to use onPause() and onResume(). I found something like goOffline(), but I cannot found this method in the new Firebase.


Solution

  • Yes, you may use the activity lifecycle methods like onPause(), onStop() on onDestroy() to close your connection.

    Also, it seems Firebase still has a goOffline() method - https://www.firebase.com/docs/android/api/#firebase_goOffline.