Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why onResume is called when i open for the first time the tab of a tabhost?

I have a tabhost with some tabs, and each tab have implemented the method onresume, because I need to reload all the data from a remote database each time the user enter again in a tabhost, not only the first time he opens it.

Ok, it works nice, but the problem is that when the user opens for the first time a tab, the two methods, onCreate and onResume are called, then, my app connect two times into the database to retrieve the info.... I want only to be called onCreate when the user enter for the first time into the tabhost.

How to avoid this rare problem?

like image 237
NullPointerException Avatar asked Dec 17 '10 18:12

NullPointerException


1 Answers

As stated on the Activity lifecycle docs, onCreate and onResume will always both be called the first time an Activity is started. When going back to Activity, at least onResume will be called, but onCreate may be called again if Android needed to free up resources.

If you need the setup to occur every time you return to the activity, why not only put the logic in onResume?

like image 163
Cheryl Simon Avatar answered Sep 28 '22 00:09

Cheryl Simon