[英]Restarting an activity in a single tab in a TabActivity?
I have a TabActivity. 我有一个TabActivity。 Each tab point to a sub activity.
每个选项卡都指向子活动。 This works great.
这非常有效。
Is there any clever way to refresh one of the activity tabs? 是否有任何聪明的方法来刷新其中一个活动标签? I just want to 'restart' the activity in tab #3 for example.
我只想“重新启动”标签#3中的活动。 Not sure of a good way to do this other than building in refresh support to the activity itself, or clearing ALL the tabs and recreating all of them.
除了构建对活动本身的刷新支持,或者清除所有选项卡并重新创建所有选项卡之外,还不确定这样做的好方法。
Thanks, 谢谢,
Slightly more dynamic solution: 稍微更动态的解决方案:
LocalActivityManager manager = getLocalActivityManager();
String currentTag = tabHost.getCurrentTabTag();
Class<? extends Activity> currentClass = manager.getCurrentActivity().getClass();
manager.destroyActivity(currentTag, true);
manager.startActivity(currentTag, new Intent(this, currentClass));
You can restart the Activity by setting flag. 您可以通过设置标志重新启动活动。 The code is as below.
代码如下。
spec = tabHost .newTabSpec("tab1")
.setIndicator("FirstActivity")
.setContent(new Intent(this,MyFirstActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);
I've not tried this myself, but typically you access each individual tab's Activity
using the LocalActivityManager
. 我自己没有尝试过,但通常使用
LocalActivityManager
访问每个单独的选项卡的Activity
。 This can be retrieved in a TabActivity
by using getLocalActivityManager()
. 可以使用
getLocalActivityManager()
在TabActivity
检索此内容。
It looks like you should be able to use destroyActivity()
and startActivity()
to restart an Activity, though I'm not exactly sure if this will work (as I've not done it myself). 看起来你应该能够使用
destroyActivity()
和startActivity()
重新启动一个Activity,虽然我不确定这是否可行(因为我自己没有这样做)。 One important thing to note is that the id
of the Activity will be equivalent to the tag you set for the tab (eg, the String you provided to TabHost.newTabSpec(String)
). 需要注意的一件重要事情是,Activity的
id
将等同于为tab设置的标记(例如,您提供给TabHost.newTabSpec(String)
)。
LocalActivityManager manager = getLocalActivityManager();
manager.destroyActivity("tab3", true);
manager.startActivity("tab3", new Intent(this, ThirdTab.class));
Here is the solution: 这是解决方案:
tabHost.setOnTabChangedListener(this);
public void onTabChanged(String tabId) {
Log.d(LOG_KEY, tabId);
LocalActivityManager manager = getLocalActivityManager();
manager.destroyActivity("ID_1", true);
manager.startActivity("ID_1", new Intent(this, YourMyActivity.class));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.