Joel Robinson-Johnson
Joel Robinson-Johnson

Reputation: 939

Calling Firebase Event Listeners in onCreate of Fragment

So, I was reading this post from the firebase blog and something caught my eye. They mentions adding the event listeners in onStart(). Typically I add my listeners in onCreate(), so that when the information needs to be shown when onResume is called it'll definitely be there (or atleast it should). Is there any perceivable difference between adding an event listener in onCreate() versus adding one in onStart()? How would I test to see which one is better?

Upvotes: 1

Views: 317

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317712

Don't use onCreate/onDelete, unless you want your listeners to still be listening when the user leaves your app by using the home button, costing you money and user users data. onStart/onStop defines the visible time of your activity, which is why it's conventional to use those for things that actively put stuff on screen.

onStart is called before onResume when the activity is becoming visible. Not sure why that would be any better than onCreate if you're concerned about the timing on onResume. BTW you can't guarantee that data will be available any any point. It's dependent on the speed and latency of the device's network connection.

Upvotes: 2

Related Questions