private void addTabToGroup(TiUITabGroup tg, TabProxy tab) {
    TiTabActivity tta = weakActivity.get();
    if (tta == null) {
      if (DBG) {
        Log.w(LCAT, "Could not add tab because tab activity no longer exists");
      }
    }
    Drawable icon =
        TiDrawableReference.fromObject(getTiContext(), tab.getProperty(TiC.PROPERTY_ICON))
            .getDrawable();
    String tag = TiConvert.toString(tab.getProperty(TiC.PROPERTY_TAG));
    String title = TiConvert.toString(tab.getProperty(TiC.PROPERTY_TITLE));
    if (title == null) {
      title = "";
    }
    tab.setTabGroup(this);
    final WindowProxy vp = (WindowProxy) tab.getProperty(TiC.PROPERTY_WINDOW);
    vp.setTabGroupProxy(this);
    vp.setTabProxy(tab);
    if (tag != null && vp != null) {
      TabSpec tspec = tg.newTab(tag);
      if (icon == null) {
        tspec.setIndicator(title);
      } else {
        tspec.setIndicator(title, icon);
      }

      Intent intent = new Intent(tta, TiActivity.class);
      vp.fillIntentForTab(intent);

      tspec.setContent(intent);

      tg.addTab(tspec);
    }
  }
 @Kroll.setProperty(runOnUiThread = true)
 @Kroll.method(runOnUiThread = true)
 public void setActiveTab(Object tab) {
   if (peekView() != null) {
     TiUITabGroup tg = (TiUITabGroup) peekView();
     tg.changeActiveTab(tab);
   } else {
     // handles the case where the setActiveTab is called before the TabGroup has finished opening
     // and thus would prevent the initial tab from being set
     initialActiveTab = tab;
   }
 }
  public void handlePostOpen(Activity activity) {
    ((TiTabActivity) activity).setTabGroupProxy(this);
    this.weakActivity = new WeakReference<TiTabActivity>((TiTabActivity) activity);
    TiUITabGroup tg = (TiUITabGroup) view;
    if (tabs != null) {
      for (TabProxy tab : tabs) {
        addTabToGroup(tg, tab);
      }
    }
    tg.changeActiveTab(initialActiveTab);

    opened = true;
    fireEvent(TiC.EVENT_OPEN, null);
  }
  @Kroll.getProperty
  @Kroll.method
  public TabProxy getActiveTab() {
    TabProxy activeTab = null;

    if (peekView() != null) {
      TiUITabGroup tg = (TiUITabGroup) peekView();
      int activeTabIndex = tg.getActiveTab();

      if (activeTabIndex < 0) {
        Log.e(LCAT, "unable to get active tab, invalid index returned: " + activeTabIndex);
      } else if (activeTabIndex >= tabs.size()) {
        Log.e(LCAT, "unable to get active tab, index is larger than tabs array: " + activeTabIndex);
      }
      activeTab = tabs.get(activeTabIndex);
    } else {
      if (initialActiveTab instanceof Number) {
        int tabsIndex = TiConvert.toInt(initialActiveTab);
        if (tabsIndex >= tabs.size()) {
          activeTab = tabs.get(tabsIndex);
        } else {
          Log.e(LCAT, "Unable to get active tab, initialActiveTab index is larger than tabs array");
        }
      } else if (initialActiveTab instanceof TabProxy) {
        activeTab = (TabProxy) initialActiveTab;
      } else {
        Log.e(LCAT, "Unable to get active tab, initialActiveTab is not recognized");
      }
    }

    if (activeTab == null) {
      String errorMessage =
          "Failed to get activeTab, make sure tabs are added first before calling getActiveTab()";
      Log.e(LCAT, errorMessage);
      throw new RuntimeException(errorMessage);
    }
    return activeTab;
  }