How to enable Firebase Real time database Persistence Data in all activity?

Very common question I found on internet and I faced this issue with real time data base. Stack overflow is filled with this question. Because firestore provide by default this but for real time database we will need to enable data persistence.

FbUtility.java

 package com.android.boom;

import com.google.firebase.database.FirebaseDatabase;

public class FbUtility {
    private static FirebaseDatabase mDatabase;

    public static FirebaseDatabase getDatabase() {
        if (mDatabase == null) {
            mDatabase = FirebaseDatabase.getInstance();
            mDatabase.setPersistenceEnabled(true);
        }
        return mDatabase;
    }

}

This is the base solution. Just create a class and then whenever needed just call this, like below in any activity

DatabaseReference usersRef = getDatabase().getReference().child("YOUR_FIREBASE_CHILD")

Conclusion

Firebase is great Platform provided by google. It make life easier and with offline support it even more simpler to make offline app with realtime data sync facilities.

Let me know in comments what do you think…