According to the activity lifecycle, onCreate() is called once when the app is created, followed by the onStart() method which may be called multiple times throughout the activity lifecycle. Yet that's not whats happening to me.
I have this code inside my onCreate method:
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Gig thisGig = dataSnapshot.getValue(Gig.class);
loadedGigs.add(thisGig);
Log.w("onCreate", "There are " + loadedGigs.size() + "Gigs loaded in the array.");
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
And then I have my onStart method:
@Override
protected void onStart()
{
super.onStart();
Log.w("onStart", "There are " + loadedGigs.size() + "Gigs loaded in the array.");
}
And here are the logs:
V/FA: onActivityCreated
W/onStart: There are 0Gigs loaded in the array.
V/FA: Activity resumed, time: 2460468116
D/FA: Logging event (FE): screen_view(_vs), Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=search_gigs_activity, ga_previous_id(_pi)=-5229767692724064313, ga_screen_class(_sc)=search_gigs_results_activity, ga_screen_id(_si)=-5229767692724064312}]
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/onCreate: There are 1Gigs loaded in the array.
W/onCreate: There are 2Gigs loaded in the array.
W/onCreate: There are 3Gigs loaded in the array.
V/FA: Inactivity, disconnecting from the service
I had other code but i deleted it all because I kept getting a null pointer exception and couldn't figure out why. Turns out I was trying to access the "loadedGigs" array list from inside the onStart method thinking it had already been populated from the onCreate method.
Any ideas on why this is happening? My best guess is it has something to do with my use of Firebase and the child event listener. I'm lacking quite a bit of knowledge and I think that's why I can't figure it out.
Thx in advance, Vlad