Android:如何重新启动 tabhost 中的活动?

发布于 2024-08-17 11:48:35 字数 1870 浏览 5 评论 0原文

我已经搜索过,我知道似乎有些人不赞成在选项卡中使用活动,但超越这一点......我如何重新启动选项卡式活动,同时仍然保持选项卡可见?我在选项卡中有一个活动,我使用菜单创建一个新活动来更新选项卡的活动显示的信息,当我从菜单活动返回时,我希望新信息显示在选项卡的活动中。我正在菜单选项中使用 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 技术交流群。

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

发布评论

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

评论(5

南笙 2024-08-24 11:48:35

这是解决方案:

tabHost.setOnTabChangedListener(this);
public void onTabChanged(String tabId) {
        LocalActivityManager manager = getLocalActivityManager();
        manager.destroyActivity("ID_1", true);
        manager.startActivity("ID_1", new Intent(this, YourMyActivity.class));
    }

Here is the SOLUTION:

tabHost.setOnTabChangedListener(this);
public void onTabChanged(String tabId) {
        LocalActivityManager manager = getLocalActivityManager();
        manager.destroyActivity("ID_1", true);
        manager.startActivity("ID_1", new Intent(this, YourMyActivity.class));
    }
沐歌 2024-08-24 11:48:35

有些人不赞成使用
选项卡内的活动

嗨!我是“一些人”!

如何重新启动选项卡式活动,同时仍保持选项卡可见?

你不知道,据我所知。

当然,由于您是finish() 并重新启动活动的人,因此您可以通过注释掉这两行代码来轻松阻止这种情况发生。您的问题不在于您丢失了选项卡,而在于您正在用大锤粉碎您的活动,而可能有更好的方法来实现您想要实现的任何类型的“刷新”。

当然,如果您的选项卡使用的是 Views 而不是 Activities,那么进行此类刷新可能会更容易,这就是“有些人不赞成使用活动”的原因之一在选项卡内”。

some people frown upon using
activities within tabs

Hi! I'm "some people"!

how would I restart a tabbed activity while still keeping the tabs visible?

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 of Activities, which is one of the reasons "some people frown upon using activities within tabs".

如痴如狂 2024-08-24 11:48:35

是的,我认为可以肯定地说,作为选项卡中的活动完成并重新启动自己不是受支持的用例。相反,当您知道“配置文件已更新”时,是否有更好的更细粒度的方法来刷新其状态?例如,查询内容提供商以刷新活动中表示的信息?这完全取决于活动中表示的信息。

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.

迷鸟归林 2024-08-24 11:48:35

嗯……我认为这些活动也不一定是个好主意。但请注意,任何活动都可以注册广播接收器,并且任何活动都可以发送广播......也许您可以注册广播接收器并以这种方式进行通信。

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.

烟若柳尘 2024-08-24 11:48:35

我建议你做这样的事情(不延长TABACTIVITY):

mlam = new LocalActivityManager(this, false);
final TabHost tabHost = (TabHost) findViewById(R.id.tabhostfaces);
mlam.dispatchCreate(bundle);
mlam.dispatchResume();
mlam.dispatchPause(isFinishing());
tabHost.setup(mlam);

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        String currentTag = mTabHost.getCurrentTabTag();
        if(currentTag.equals("tab_ntflist")){
            ntfreglst.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            mlam.startActivity(currentTag, new Intent(ntfreglst));
        } else if(currentTag.equals("tab_profile")){
            pflvw.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            mlam.startActivity(currentTag, new Intent(pflvw));
        }
    }
});

其中 ntfreglst 和 pflvw 是已经定义的意图。
该标志表示,当您调用startActivity时,由于该活动已经在运行,因此只会调用onResume方法
这样,每次选项卡更改时,都会调用 ONRESUME 方法。这样您就可以在 onResume 方法中进行所有更新。

I suggest you to do something like this (NOT EXTENDING TABACTIVITY):

mlam = new LocalActivityManager(this, false);
final TabHost tabHost = (TabHost) findViewById(R.id.tabhostfaces);
mlam.dispatchCreate(bundle);
mlam.dispatchResume();
mlam.dispatchPause(isFinishing());
tabHost.setup(mlam);

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        String currentTag = mTabHost.getCurrentTabTag();
        if(currentTag.equals("tab_ntflist")){
            ntfreglst.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            mlam.startActivity(currentTag, new Intent(ntfreglst));
        } else if(currentTag.equals("tab_profile")){
            pflvw.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            mlam.startActivity(currentTag, new Intent(pflvw));
        }
    }
});

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.

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