Restarting an activity in a single tab in a TabActivity?

后端 未结 4 584
渐次进展
渐次进展 2020-12-29 16:06

I have a 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\' th

相关标签:
4条回答
  • 2020-12-29 16:40

    I've not tried this myself, but typically you access each individual tab's Activity using the LocalActivityManager. This can be retrieved in a TabActivity by using getLocalActivityManager().

    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). One important thing to note is that the id of the Activity will be equivalent to the tag you set for the tab (e.g., the String you provided to TabHost.newTabSpec(String)).

    LocalActivityManager manager = getLocalActivityManager();
    manager.destroyActivity("tab3", true);
    manager.startActivity("tab3", new Intent(this, ThirdTab.class));
    
    0 讨论(0)
  • 2020-12-29 16:46

    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));
        }
    
    0 讨论(0)
  • 2020-12-29 16:47

    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));
    
    0 讨论(0)
  • 2020-12-29 16:49

    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);
    
    0 讨论(0)
提交回复
热议问题