简体   繁体   English

在TabActivity的单个选项卡中重新启动活动?

[英]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.

相关问题 重新启动Tabactivity中的单个活动 - Restart single activity within a tabactivity 如何防止在TabActivity的第一个选项卡上启动活动? - How to prevent starting the activity at the first tab in a TabActivity? 如何避免在TabActivity的第一个选项卡上启动活动? - How to avoid starting the activity at the first tab in a TabActivity? 触摸选项卡小部件时,TabActivity无法启动活动 - TabActivity not starting activity when tab widget touched 如何通过“活动”(标签)在TabActivity中设置控件的可见性? - How to setVisibility for control in TabActivity from Activity (tab)? 选项卡更改时,TabActivity销毁每个选项卡的活动 - TabActivity destroy Activity of each tab when tab changed 在android中更改的选项卡上没有重新启动活动 - Activity is not restarting on tab changed in android 如何从tabactivity开始的活动中更改tabactivity的选项卡? 或更改当前选项卡 - how to change tab of a tabactivity from an activity started by the tabactivity ? or change current tab 有没有办法添加标签布局而不让活动扩展TabActivity? - Is there any way to add tab layout without letting the activity to extend TabActivity? 是否有任何好的解决方案分别在TabActivity中为每个活动进行搜索? - Is there any good solution for searching in TabActivity for each activity in tab respectively?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM