Why is onResume called when I open the tabhost tab for the first time?

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

ok, it works well, but the problem is that when the user first opens the tab, two methods are called: onCreate and onResume, then my application connects to the database twice to retrieve the information .... I only want to be called onCreate when the user first introduces into tabhost

How to avoid this rare problem?

+5
source share
3 answers

As stated in the Activity Lifecycle Docs, onCreate and onResume will always be called when Activity is first run. When you return to Activity, at least onResume is called, but onCreate can be called again if Android needs to free resources.

If you want the configuration to occur every time you return to activity, why not put the logic in onResume?

+14
source

Since the contents of your tabs are actions, they must both be created (and resumed) when the application starts.

. , 1 onCreate(), 1 (TabActivity).

0

I agree with other posters that you should redefine your application. you can't just expect to embed offline activity in a tab and have everything ok.

that, as they say, you can have a flag somewhere that indicates whether the database is needed for initialization. in every onResume () action you have something like

synchronized (MyLock.class) {
    if (!initialized) {
      initDb();
      MyLock.initialized = true;
    }
}
0
source

All Articles