The problem: the layout in my MainActivity is generated before I have a chance to finish calling the firebase to restore app data. If I rotate the screen, thus causing onCreate to run again in the MainActivity, everything is generated just fine.
In my app, I have a custom implementation of the Application class, which makes a bunch of calls to Firebase in order to restore data / make sure data is always in sync. However, instead of having just a few ValueEventListeners with a ton of children underneath, I have around 20 ValueEventListeners. This is to prevent my app from syncing nearly the entire database every time the user generates a tiny bit of data, and to avoid conflicts that can happen when data is manipulated asynchronously. Interestingly, the ValueEventListeners don't actually fetch their data in the order that they're coded in, so I can't set a bool to true when the last one is done.
I'm wondering if there's a simple way to detect whether the firebase reads are all done, other than placing some code at the end of every single Listener and performing an operation that does this manually. I've looked at the methods available in the Application class, as well as Firebase, and so far I haven't found anything that works.
some code from my Application class:
public class CompassApp extends Application {
... then inside the Application's onCreate:
// Fetching Data from DB.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dbRef = database.getReference();
// Current User Data
dbRef.child("currentAppData").child("workingOut").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
activeFirebaseConnections += 1;
// Stops executing method if there is no data to retrieve
if (!dataSnapshot.exists()) {
return;
}
workingOut = dataSnapshot.getValue(boolean.class);
activeFirebaseConnections -= 1;
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "Firebase read of sleepDebt failed");
}
});
dbRef.child("currentAppData").child("sleeping").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
activeFirebaseConnections += 1;
// Stops executing method if there is no data to retrieve
if (!dataSnapshot.exists()) {
return;
}
sleeping = dataSnapshot.getValue(boolean.class);
activeFirebaseConnections -= 1;
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "Firebase read of sleepDebt failed");
}
});
(and so on... the rest is just more ValueEventListeners)