简体   繁体   中英

Firebase Database Android - Adding an app-wide ValueEventListener

I want my Android app to be able to listen to a Firebase Database reference from the point that the app starts, until the app terminates.

The desired behavior is achieved by adding a ValueEventListener at the Database reference in Application's onCreate and removing it in Application's onTerminate :

public class My_Application extends Application {

// The DB ref that the ValueEventListener will be added to.
DatabaseReference dbRef = V_DB.FBREF_CHATROOMS;

// Used to remove ValueEventListeners that were added in this class.
Pair<DatabaseReference, ValueEventListener> mListener;

@Override
public void onCreate() {
    super.onCreate();

    // Add the ValueEventListener.
    ValueEventListener mValueEventListener = dbRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Do something here.
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Do something here.
        }
    });

    // Store listener.
    mListener = new Pair<>(dbRef, mValueEventListener);
}

@Override
public void onTerminate() {
    super.onTerminate();
    // Remove listener.
    mListener.first.removeEventListener(mListener.second);
}

}

It seems like the obvious way to go, but as stated here for Application.onCreate :

Implementations should be as quick as possible (for example using lazy initialization of state) since the time spent in this function directly impacts the performance of starting the first activity, service, or receiver in a process.

Considering the above (and not excluding other potential problems), I would like to ask if my approach is the way to go when you want an app-wide ValueEventListener or is there another more efficient approach.

Following Ronak Makwana 's suggestion, I used a Service to add all my app-wide ValueEventListeners in my SplashScreenActivity's onCreate() . The Service is stopped in my SplashScreenActivity's onDestroy() . In my Service's onDestroy() , I remove all app-wide ValueEventListeners that I've added.

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Do other stuff

    // Start service for adding App-Wide Listeners.
    this.startService_AddAppwideValuEventListeners();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    // Stop Service (by doing so, all App-Wide Listenersare removed in Service's onDestroy().
    stopService(new Intent(this, Service_AddAppwideValuEventListeners.class));
}

// Start Service
private void startService_AddAppwideValuEventListeners() {
    // Get Intent for starting my Service that adds some VELs.
    Intent mServiceIntent = new Intent(this, Service_AddAppwideValuEventListeners.class);

    // Start service (this service is stopped in Activity's onDestroy())
    wCurrentContext.startService(mServiceIntent);
}

So far, it works like a charm and the overhead while doing that is quite low (since everything is done in a Service).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM