问题描述
我在这里继例如:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
一切工作正常。我第一次点击每个选项卡上,绑定到特定的标签活动中的的OnCreate
方法被调用。然而,标签的的后续选择不调用此onCreate方法。
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.
我需要能够执行的OnCreate(或其他方式)的绑定到每个选项卡,在选择该选项卡的活动。我知道我可以使用 setOnTabChangedListener
,但我不能确定如何获得访问绑定到该选项卡中的活动,这样我可以调用的OnCreate(或其他)方法。
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.
推荐答案
这是一个效率问题...这就是为什么你的的onCreate
方法不会被调用两次或两次以上倍。该eaiser办法从访问您的活动你的 TabActivity
通过 OnTabChangedListener
是这样的:
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 your TabActivity
through the OnTabChangedListener
is this:
public class YourTabActivity extends TabActivity{
public void onCreate(Bundle InSavedInstanceState) {
super.onCreate(InSavedInstanceState);
final TabHost tabHost = getTabHost();
// blablabla
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
if( tabId.equals("the_id_of_your_tab") ){
NameOfThatActivity.self.theMethodYouWantToCall();
}
}
});
}
}
然后,在你的孩子的活动,你有这样的:
Then, on your child activity, you have something like:
public class NameOfThatActivity extends Activity{
public static NameOfThatActivity self;
// blah blah blah
public onCreate(Bundle b){
super.onCreate(b);
self = this;
}
public void theMethodYouWantToCall(){
// do what ever you want here
}
}
这不是美,而是它工作正常。
It's not beauty, but it works fine.
这篇关于TabHost setCurrentTab仅适用于标签的Activity调用onCreate方法一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!