一个tabwidget内活动的生命周期 [英] Activity lifecycle within a tabwidget

查看:105
本文介绍了一个tabwidget内活动的生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择相关选项卡时通知我的活动。
考虑follwoing情况:

I'm trying to notify my activity when the relevant tab is selected. Consider the follwoing situation:

MainActivity:

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, HomeActivity.class);

// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("Home",
                  res.getDrawable(R.drawable.ic_tab_home))
              .setContent(intent);

tabHost.addTab(spec);

// Do the same for the other tabs
intent = new Intent().setClass(this, ProceduresActivity.class);
spec = tabHost.newTabSpec("procedures").setIndicator("Procedures",
                  res.getDrawable(R.drawable.ic_tab_checklist))
              .setContent(intent);
tabHost.addTab(spec);

现在,第一次选择我的第二个选项卡,的onCreate方法 ProceduresActivity 被调用。
我想alble做的是,在 ProceduresActivity 后,只要卡1和卡2(矿)被选中的通知。
我trye​​d覆盖的onPause和onResume ProceduresActivity 然而,当在 MainActivity 的pasued /恢复那些只被调用,而不是当我切换选项卡中...
因此,换句话说,我想的 ProceduresActivity 做一些事情,只要用户选择了第二个选项卡,不仅第一次。
有人能帮助我吗?
谢谢,卢卡。

Now, the first time my second tab is selected, the onCreate method of ProceduresActivity is called. What I want to be alble to do is, within ProceduresActivity, to be notified whenever tab 1 and tab 2 (mine) are selected. I tryed overriding onPause and onResume within ProceduresActivity however those are only called when the MainActivity is pasued / resumed and not when I switch tabs... So in other words I want ProceduresActivity to do something whenever the user selects second tab, and not only the first time. Can someone help me please? Thanks, Luca.

推荐答案

嗯。这似乎是它可能是一个有点困难(​​我什至不知道这是可能的)来实现。有两种方法,我可以看到,你可以达到同样的效果。

Hmmm. This seems like it might be a little difficult (I'm not even sure it's possible) to achieve. There are two ways I can see that you could achieve the same results.

第一种方式是由具有第一个选项卡刷新它的数据/信息,当用户返回到它。这可以在onResume(),并通过添加一个标志,它做。 pretty简单。

The first way is by have the first tab refresh it's data/information when the user goes back to it. This can be done in the onResume() and by adding a flag to it. Pretty simple.

如果你需要你的活动做一些事情,需要一定的时间,可以使一个的ActivityGroup。它基本上是多个活动的控制器。它开始他们都在同一时间,只是选择哪一个立刻显示。这意味着,(你的情况)两者的活动将被运行,但只有一个将被示出。当你想第一个选项卡,而用户使用的第二个选项卡做一些事情,你去的ActivityGroup(这就像一个控制器),并​​告诉它做任何你想要的第一个标签。

If you need your activity to do something that will take some time, you can make an ActivityGroup. It's basically a controller for multiple activities. It starts them all at the same time and just chooses which one is shown at once. This means that (in your case) both activities would be running, but only one would be shown. When you want the first tab to do something while the user is using the second tab, you go to the ActivityGroup (which is like a controller) and tell it to do whatever you want with the first tab.

我用的ActivityGroup路线看到的唯一问题是,它绑定到你的标签,但也有许多变通办法此。

The only problem I see with the ActivityGroup route is binding it to your tabs, but there are many workarounds for this.

这篇关于一个tabwidget内活动的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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