TabHost setCurrentTab 只调用 Tab 中 Activity 的 oncreate 方法一次

发布于 2024-09-18 17:38:32 字数 453 浏览 6 评论 0原文

我在这里遵循示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

金橙橙 2024-09-25 17:38:32

这是一个效率问题...这就是为什么您的 onCreate 方法不会被调用两次或更多次。通过 OnTabChangedListenerTabActivity 访问您的 Activity 的更简单方法是:

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();
                }
            }
        });
    }
}

然后,在您的子 Activity 上,您会看到如下内容:

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 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.

紫罗兰の梦幻 2024-09-25 17:38:32

看看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)

烟柳画桥 2024-09-25 17:38:32

如果使用 TabActivity.getCurrentActivity() 则可以调用另一个方法

another method you can call if use TabActivity.getCurrentActivity()

幼儿园老大 2024-09-25 17:38:32

正如@Cristian 所指出的,这是一个效率问题。
但您始终可以在子活动中使用 onResume() 方法。

@Override
protected void onResume() {
     super.onResume();               
     // do work 

}

As pointed out by @Cristian, it's a matter of efficiency.
but you can always use the onResume() method in your child activity instead.

@Override
protected void onResume() {
     super.onResume();               
     // do work 

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文