Why does the first tab activity in Android live fo

2019-05-10 00:55发布

问题:

I've created a simple TabActivity which builds tabs at runtime reading some JSON data. At the moment I'm initializing an empty ListActivity with dummy random items for each tab just to see that changing from tab to tab works and the content doesn't disappear. The actual content for the tabs is stored in a singleton class, so when the tab activities get re-created due to a screen orientation change, they just pull the correct dummy items from the respective list according to an identifier contained in the intent's extras bundle.

Everything works fine. I've put a log on the list activity onCreate method to watch the activities get recreated on screen rotation, and they do. However, there is something strange in that the activity for the first tab is always recreated, even if it is not visible. When I switch to the third or fourth tab and rotate the device, the previous tabs get killed and don't get recreated again, except the first tab. The first tab is always there. Why? I would understand a bug in the code, but the tab activity is copied from a tutorial and the list activity is the same for all tabs. Here are some logs I get:

List created with 1
onConfigurationChanged
List created with 1
List created with 2
List created with 4
List created with 5
List created with 6
onConfigurationChanged
List created with 1
List created with 6
onConfigurationChanged
List created with 1
List created with 6
onConfigurationChanged
List created with 1
onConfigurationChanged
List created with 1
List created with 2
onConfigurationChanged
List created with 1
List created with 2
onConfigurationChanged
List created with 1
List created with 2
List created with 4
onConfigurationChanged
List created with 1
List created with 4
onConfigurationChanged
List created with 1
List created with 4
List created with 5
onConfigurationChanged
List created with 1
List created with 5
onConfigurationChanged
List created with 1
List created with 5

As you can see from the logs, each list activity logs the identifier put in the extras bundle, and does pretty much nothing else. So the application starts and the first tab gets created, then I rotate and switch to the other tabs. Their onCreate method is called. And then the log shows how I'm switching tabs and rotating. Different activities get recreated depending on which tab is visible, but the first one is always there!

Why is the first tab always being recreated? Is this special behavior needed for some reason? I'm seeing this on an HTC Legend API level 7.

UPDATE: I've put some more logs in the TabActivity's loop creating the individual tabs, and it looks like when the first tab is added its activity is always created. Is there any way to avoid this? Maybe create dummy empty tabs and later populate them with the real activities? Some more logs:

onConfigurationChanged
Tab: Saving tab index 3
...
Tab: Adding tab 1
List created with 1
Tab: Adding tab 2
Tab: Adding tab 3
Tab: Adding tab 4
Tab: Adding tab 5
Tab: Setting tab to index 3
List created with 5

回答1:

After putting some more logs in the activity's lifecycle methods, I found out that the onCreate() and onStart() methods are always created for the first tab, but the onResume() method is only called for the visible tab. So I will put my lazy initialisation code on the onResume() method to avoid the first activity bog down the application.

Possibly the TabActivity works internally in a similar way to a http://developer.android.com/reference/android/widget/ViewFlipper.html and that's why the first tab activity is always created despite not being visible.



回答2:

Try changing the initial tab to 2..n and see if that is the tab that gets created. I am betting it has something to do with how Android handles the TabActivity. If that's true then you might be able to check which tab you're on for != null and use that as the first tab or default to 1 if null.