Keep your Firebase Realtime Database connections to minimum

Kirtan Thakkar

Kirtan Thakkar

Life is all about learning

Creating a process creates an persistence connection to the firebase, even if you app is in the background. So, disconnecting that open connection to the database when your app is not in use will save data as well as your precious simultaneous connections if you are on free Spark plan.

For this, to implement on your Android app, we can use the Activity Lifecycle Callbacks and disconnect the connection when the app is in the background.

Look at the below snippet:

FirebaseDatabaseConnectionHandler.java
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import com.google.firebase.database.FirebaseDatabase;
public class FirebaseDatabaseConnectionHandler implements Application.ActivityLifecycleCallbacks {
private static final String TAG = FirebaseDatabaseConnectionHandler.class.getSimpleName();
private int count = 0;
private final long delayedTimeMillis = 5000; // change this if you want different timeout
private Handler mHandler = new Handler();
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
@Override
public void onActivityStarted(Activity activity) {
count++;
Log.d(TAG, "onActivityStarted: count=" + count);
if (count > 0)
FirebaseDatabase.getInstance().goOnline();
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
count--;
Log.d(TAG, "onActivityStopped: count=" + count);
if (count == 0) {
Log.d(TAG, "onActivityStopped: going offline in 5 seconds..");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// just make sure that in the defined seconds no other activity is brought to front
Log.d(TAG, "run: confirming if it is safe to go offline. Activity count: " + count);
if (count == 0) {
Log.d(TAG, "run: going offline...");
FirebaseDatabase.getInstance().goOffline();
} else {
Log.d(TAG, "run: Not going offline..");
}
}
}, delayedTimeMillis);
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
}
MyApplication.java
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// your stuff here
// Manage firebase database connection when in background and foreground
registerActivityLifecycleCallbacks(new FirebaseDatabaseConnectionHandler());
}
}

This is really simple. So, after your app goes into the background, it will wait for the milliseconds defined in delayedTimeMillis and after that it will disconnect the connection. If in between that time, your app is again brought back into the foreground, it will not disconnect the connection.

There is also one another very similar approach from the Doug Stevenson here. Whatever you use will do the same thing.

Library Available

Update: I have built a library for simplified background checking. This can be helpful for detecting background state changing. Check out BackgroundManager

So, go ahead, save your connections and save user's data. #BuildBetterApps