TabHost setCurrentTab 只调用 Tab 中 Activity 的 oncreate 方法一次
我在这里遵循示例:
http://developer.android.com /resources/tutorials/views/hello-tabwidget.html
一切正常。第一次单击每个选项卡时,将调用绑定到该特定选项卡的 Activity 的 oncreate
方法。但是,选项卡的后续选择不会调用此 oncreate 方法。
当选择该选项卡时,我需要能够在绑定到每个选项卡的活动上执行 oncreate (或其他方法)。我知道我可以使用 setOnTabChangedListener,但我不确定如何访问绑定到选项卡的 Activity,以便我可以调用 oncreate(或其他)方法。
I'm following the example here:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
Everything works fine. The first time I click on each tab, the oncreate
method for Activity bound to that particular tab is called. However, subsequent selections of the tab's do not call this oncreate method.
I need to be able to execute oncreate (or another method) on the Activity that is bound to each Tab, when that tab is selected. I know I can use a setOnTabChangedListener
, but I am unsure how to get access to the Activity that is bound to the tab, so that I can call the oncreate (or another) method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个效率问题...这就是为什么您的
onCreate
方法不会被调用两次或更多次。通过OnTabChangedListener
从TabActivity
访问您的 Activity 的更简单方法是:然后,在您的子 Activity 上,您会看到如下内容:
它并不美观,但效果很好。
It's a matter of efficiency... that's why your
onCreate
method is not being called twice or more times. The eaiser way to access your activity from yourTabActivity
through theOnTabChangedListener
is this:Then, on your child activity, you have something like:
It's not beauty, but it works fine.
看看Activity类中的onStart方法,我认为你是想要覆盖它而不是 onCreate (或者除此之外,通常您仅在 onCreate 中调用 setContentView )
Look at the onStart method in the Activity class, I think you are wanting to override that instead of onCreate (or in addition to, typically you call setContentView only in onCreate)
如果使用 TabActivity.getCurrentActivity() 则可以调用另一个方法
another method you can call if use TabActivity.getCurrentActivity()
正如@Cristian 所指出的,这是一个效率问题。
但您始终可以在子活动中使用 onResume() 方法。
As pointed out by @Cristian, it's a matter of efficiency.
but you can always use the onResume() method in your child activity instead.