为什么Android中的第一个标签页活动永远存在? [英] Why does the first tab activity in Android live forever?

查看:102
本文介绍了为什么Android中的第一个标签页活动永远存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的TabActivity,它在运行时会构建一些选项卡,以读取一些JSON数据.目前,我正在初始化一个空的ListActivity,其中每个选项卡都带有虚拟随机项,只是为了看到从一个选项卡到另一个选项卡的更改都起作用并且内容不会消失.选项卡的实际内容存储在singleton类中,因此,由于屏幕方向更改而重新创建了选项卡活动时,它们只是根据意图附加项目包中包含的标识符从相应列表中拉出正确的虚拟项目.

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.

一切正常.我在list活动的onCreate方法上放置了一个日志,以观看在屏幕旋转时重新创建的活动,它们确实如此.但是,有些奇怪之处在于,即使第一个选项卡的活动不可见,也始终会重新创建它.当我切换到第三个或第四个选项卡并旋转设备时,除第一个选项卡外,先前的选项卡被杀死并且不再重新创建.第一个选项卡始终存在.为什么?我会理解代码中的错误,但是选项卡活动是从教程中复制的,并且列表活动对于所有选项卡都是相同的.这是我得到的一些日志:

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

从日志中可以看到,每个列表活动都会记录附加包中放置的标识符,并且几乎不执行其他任何操作.因此,应用程序启动并创建了第一个选项卡,然后旋转并切换到其他选项卡.他们的onCreate方法被调用.然后日志显示了我如何切换标签和旋转.根据显示的选项卡,可以重新创建不同的活动,但是第一个始终存在!

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!

为什么总是总是重新创建第一个标签?是否出于某种原因需要这种特殊行为?我在HTC Legend API级别7上看到了这一点.

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.

更新:我已经在TabActivity循环中添加了一些日志,以创建各个标签,看起来当添加第一个标签时,其活动总是创建 .有什么办法可以避免这种情况?也许创建虚拟的空选项卡,然后再用实际活动填充它们?一些日志:

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

推荐答案

在活动的生命周期方法中添加了更多日志后,我发现始终为第一个选项卡创建onCreate()和onStart()方法,但是仅针对可见选项卡调用onResume()方法.因此,我将懒惰的初始化代码放在onResume()方法上,以避免第一次活动使应用程序陷入困境.

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.

TabActivity可能在内部以类似于 http://developer的方式工作.android.com/reference/android/widget/ViewFlipper.html ,这就是为什么尽管看不见总是创建第一个选项卡活动的原因.

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.

这篇关于为什么Android中的第一个标签页活动永远存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆