Android:如何重新启动 tabhost 中的活动?
我已经搜索过,我知道似乎有些人不赞成在选项卡中使用活动,但超越这一点......我如何重新启动选项卡式活动,同时仍然保持选项卡可见?我在选项卡中有一个活动,我使用菜单创建一个新活动来更新选项卡的活动显示的信息,当我从菜单活动返回时,我希望新信息显示在选项卡的活动中。我正在菜单选项中使用 startActivityForResult() ,但是当我返回并尝试重新启动活动时...它会清除上面的选项卡(我猜如预期的那样,但我想重新启动选项卡中刷新的活动) 。
创建选项卡:
TabHost host = getTabHost();
Intent home_intent = new Intent(constants.HOME_ACTION,
null, this, homeTab.class);
Intent inbox_intent = new Intent(constants.INBOX_ACTION,
null, this, inboxTab.class);
Intent stats_intent = new Intent(constants.STATS_ACTION, null,
this, infoTab.class);
host.addTab(host.newTabSpec(constants.HOME_TAG)
.setIndicator(getText(R.string.home_label),
getResources().getDrawable(R.drawable.icon))
.setContent(home_intent));
host.addTab(host.newTabSpec(constants.INBOX_TAG)
.setIndicator(getText(R.string.inbox_label),
getResources().getDrawable(R.drawable.icon))
.setContent(inbox_intent));
host.addTab(host.newTabSpec(constants.STATS_TAG)
.setIndicator(getText(R.string.stats_label),
getResources().getDrawable(R.drawable.icon)).setContent(
stats_intent));
从选项卡活动中的菜单活动返回(更新数据库信息):
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (constants.ACTIVITY_REQUEST_CODE_UPDATE_PROFILE) : {
if (resultCode == Activity.RESULT_OK) {
boolean profileUpdated = data.getBooleanExtra(constants.ACTIVITY_BUNDLE_UPDATE_PROFILE, false);
Log.d(LOG_TAG, "activity returned with " + profileUpdated);
// Check to see if we updated our profile to refresh the screen
if(profileUpdated == true){
// Refresh the screen with the new info
homeTab.this.finish();
this.startActivity(getIntent());
}
}
break;
}
}
}
I've searched and I know it seems some people frown upon using activities within tabs, but moving past that...how would I restart a tabbed activity while still keeping the tabs visible? I have an activity in a tab, I use the menu to create a new activity to update the tab's activity displayed info, when I return from the menu activity I want the new information to be displayed in the tab's activity. I am using startActivityForResult() from the menu choice, but when I return and try to restart the activity...it wipes out the tabs above(I guess as expected, but I want to re-launch the refreshed activity within the tab).
Creating the tabs:
TabHost host = getTabHost();
Intent home_intent = new Intent(constants.HOME_ACTION,
null, this, homeTab.class);
Intent inbox_intent = new Intent(constants.INBOX_ACTION,
null, this, inboxTab.class);
Intent stats_intent = new Intent(constants.STATS_ACTION, null,
this, infoTab.class);
host.addTab(host.newTabSpec(constants.HOME_TAG)
.setIndicator(getText(R.string.home_label),
getResources().getDrawable(R.drawable.icon))
.setContent(home_intent));
host.addTab(host.newTabSpec(constants.INBOX_TAG)
.setIndicator(getText(R.string.inbox_label),
getResources().getDrawable(R.drawable.icon))
.setContent(inbox_intent));
host.addTab(host.newTabSpec(constants.STATS_TAG)
.setIndicator(getText(R.string.stats_label),
getResources().getDrawable(R.drawable.icon)).setContent(
stats_intent));
Return from the menu activity in the tab's activity(updating database info):
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (constants.ACTIVITY_REQUEST_CODE_UPDATE_PROFILE) : {
if (resultCode == Activity.RESULT_OK) {
boolean profileUpdated = data.getBooleanExtra(constants.ACTIVITY_BUNDLE_UPDATE_PROFILE, false);
Log.d(LOG_TAG, "activity returned with " + profileUpdated);
// Check to see if we updated our profile to refresh the screen
if(profileUpdated == true){
// Refresh the screen with the new info
homeTab.this.finish();
this.startActivity(getIntent());
}
}
break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是解决方案:
Here is the SOLUTION:
嗨!我是“一些人”!
你不知道,据我所知。
当然,由于您是
finish()
并重新启动活动的人,因此您可以通过注释掉这两行代码来轻松阻止这种情况发生。您的问题不在于您丢失了选项卡,而在于您正在用大锤粉碎您的活动,而可能有更好的方法来实现您想要实现的任何类型的“刷新”。当然,如果您的选项卡使用的是
Views
而不是Activities
,那么进行此类刷新可能会更容易,这就是“有些人不赞成使用活动”的原因之一在选项卡内”。Hi! I'm "some people"!
You don't, AFAIK.
Of course, since you're the one who is
finish()
-ing and restarting the activity, you can easily stop that from happening by commenting out those two lines of code. Your problem isn't that you're losing the tabs -- it's that you're smashing your activity with a sledgehammer when there is probably a better way of doing whatever sort of "refresh" you are trying to achieve.Of course, doing that sort of refresh would probably be easier if you had
Views
for your tabs instead ofActivities
, which is one of the reasons "some people frown upon using activities within tabs".是的,我认为可以肯定地说,作为选项卡中的活动完成并重新启动自己不是受支持的用例。相反,当您知道“配置文件已更新”时,是否有更好的更细粒度的方法来刷新其状态?例如,查询内容提供商以刷新活动中表示的信息?这完全取决于活动中表示的信息。
Yeah, I think it's safe to say that finishing and restarting yourself as an Activity within a tab is not a supported use case. Instead, when you know a "profile has been updated", is there a better finer grained way to refresh its state? E.g query a content provider to refresh the information represented in the activity? It all depends on what information is being represented in the Activity.
嗯……我认为这些活动也不一定是个好主意。但请注意,任何活动都可以注册广播接收器,并且任何活动都可以发送广播......也许您可以注册广播接收器并以这种方式进行通信。
Well ... I don't think the activities are necessarily a good idea either. Please note however that any activity can register a broadcast receiver, and any activity can send broadcasts .... Perhaps you could register a broadcast receiver and communicate that way instead.
我建议你做这样的事情(不延长TABACTIVITY):
其中 ntfreglst 和 pflvw 是已经定义的意图。
该标志表示,当您调用startActivity时,由于该活动已经在运行,因此只会调用onResume方法
这样,每次选项卡更改时,都会调用 ONRESUME 方法。这样您就可以在 onResume 方法中进行所有更新。
I suggest you to do something like this (NOT EXTENDING TABACTIVITY):
Where ntfreglst and pflvw are Intents already defined.
The flag indicates that, when you call startActivity, as the activity is already running, only the onResume method will be invoked
This way, everytime the tab changes, the ONRESUME method will be invoked. This way you can make all your updates inside the onResume method.