Removing a tab and actions (intentions) inside it from TabHost

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){ // 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 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); // 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, 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.

+4
source share
2 answers

Try changing the current tab to 0.

Sort of:

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

I read that calling clearAllTabs(); will nullpointerexception if you don't set tabhost on the first tab ( .setCurrentTab(0) ) before calling ( .clearAllTabs() )

And this answer can help? ( How to remove a tab from TabHost )

+14
source

I would suggest a different approach. You can use ActivityGroup to create your own TabControl. Since you use regular buttons (or similar controls as you like), you can easily organize / create / delete them as needed.

I cannot dump all the code here, but this is basically what I did when I had the same problem:

  • Create an action inherited from ActivityGroup
  • Put the ViewGroup in your layout where you want to show supporting actions.
  • Customize your buttons as needed (LinearLayout works great with a variable number of buttons)
  • Run the action through getLocalActivityManager (). startActivity () if necessary

Now you can add / remove buttons as you wish. Activites follow the Android life cycle, so you don’t need to delete them yourself.

You may need to implement onBackPressed in your ActivityGroup in order to handle the history correctly, but it depends on the project.

+1
source

All Articles