I have an application that can dynamically create tabs. And when I create a tab, I start activity as an intention. For instance:
private void addTab(Context packageContext, Class<?> newClass, TabHost mTabHost, String tabId, String tabLabel){
Pretty standard. And it works great. Now suppose I have a button (or menuitem, whatever) in the action that I created inside my tab. When the user clicks this button, I want the activity and the tab inside it to be deleted and destroyed.
I cannot find an easy way to do this. I found the TabHost.clearAllTabs () function, but this destroys all the tabs and actions, I just want to delete them.
Someone suggested that I save the list of all openable mailboxes, and then call clearAllTabs (), after which I recreate all my other tabs except those that I donβt need.
Something like that:
public static ArrayList<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>();
I add this line to my addTab () function so that every tab I create remembers in my ArrayList:
list.add(spec);
And then, when I want to remove my tab, I launched this function:
public static void removeTab(){ list.remove(list.size()-1);
This removes my tab from my ArrayList, deletes all tabs, and then recreates all remaining tabs using my ArrayList. Theoretically, this should work, but when I try to call this function, I get the following error:
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 tries to set the current tab and falls into the null pointer exception.
If you guys can suggest another way to achieve what I want to do, or a way to fix my current method, I would appreciate it.