Pete
Pete

Reputation: 3872

Removing a tab and the activity (intent) inside of it from a TabHost

I have an app that can create tabs dynamically. And when I create a tab I initiate an activity as an intent. Like so:

private void addTab(Context packageContext, Class<?> newClass, TabHost mTabHost, String tabId, String tabLabel){
    // newClass is my Activity class that I want to start in the tab
    Intent intent = new Intent().setClass(packageContext, newClass);
    TabHost.TabSpec spec;
    spec = mTabHost.newTabSpec(tabId).setIndicator(tabLabel)
                  .setContent(intent);
    mTabHost.addTab(spec);
    mTabHost.setCurrentTabByTag(tabId);
}

Pretty standard. And it works great. Now, suppose that I have a button (or menuitem, whatever) in the activity that I instantiated inside of my tab. When the user presses this button, I want the activity, and the tab it is inside of, to be removed and destroyed.

I can't seem to find a simple way to do this. I have found the TabHost.clearAllTabs() function, but this destroys all tabs and activities, I just want to remove one.

Someone suggested I save a list of all Tabs that I have opened, and then call clearAllTabs(), after which I recreate all of my other tabs except for the one I don't want.

Something like this:

public static ArrayList<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>();

I add this line to my addTab() function so that every tab I create is remember in my ArrayList:

list.add(spec);

And then when I want to remove my tab I run this function:

public static void removeTab(){
    list.remove(list.size()-1); // remove it from memory
    mTabHost.clearAllTabs();  // clear all tabs from the tabhost
    for(TabHost.TabSpec spec : list) // add all that you remember back
        mTabHost.addTab(spec);
}

This removes my tab from my ArrayList, removes all tabs, then recreates all the tabs remaining using my ArrayList. In theory it should work, but I get the following error when I try call this function:

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.TabWidget.setCurrentTab(TabWidget.java:342)
at android.widget.TabWidget.focusCurrentTab(TabWidget.java:366)
at android.widget.TabHost.setCurrentTab(TabHost.java:323)
at android.widget.TabHost.addTab(TabHost.java:216)
at com.example.myapp.TabManager.removeTab(QuikBrowser.java:86)
at com.example.myapp.TabManager.TabWindow.onOptionsItemSelected(TabWindow.java:91)
at android.app.Activity.onMenuItemSelected(Activity.java:2205)

For some reason, when adding a tab, it attempts to set the current tab, and it hits a null pointer exception.

If you guys could suggest another way of achieving what I want to do, or a way to fix my current method, I would appreciate it.

Upvotes: 4

Views: 5861

Answers (2)

wired00
wired00

Reputation: 14438

Try changing current tab to 0.

Something like:

getTabHost().setCurrentTab(0);
getTabHost().clearAllTabs();

I was reading that calling clearAllTabs(); will throw a nullpointerexception if you don't set the tabhost to the first tab (.setCurrentTab(0)) before calling (.clearAllTabs())

Also this answer may help? (How to remove tab from TabHost)

Upvotes: 14

marsbear
marsbear

Reputation: 1459

I would suggest a different approach. You can use an ActivityGroup to build your own TabControl. As you are using normal Buttons (or similar controls just as you like) you can easyly arrange/create/remove them as needed.

I can't dump the whole code here but that is basically what I did when I had the same problem:

  • Create an Activity inherited from ActivityGroup
  • Place a ViewGroup in your layout where you want to show the sub-activities
  • Setup your buttons as needed (LinearLayout works fine with a variable count of buttons)
  • Start activites thru getLocalActivityManager().startActivity() as needed

You can now add/remove buttons as you like. The Activites follow the Android lifecycle so you don't have to delete them yourself.

You might have to implement onBackPressed on your ActivityGroup to properly handle the history but that depends on the project.

Upvotes: 1

Related Questions