Hemendra Sharma
Hemendra Sharma

Reputation: 1060

Destroy or finish an Activity from tabcontent in TabHost?

I have an app where I've used Tab Host to hold multiple activities on a single screen. This is how I'm adding tabs to the Tab Host.

TabHost tabHost = getTabHost();
TabSpec tab1 = tabHost.newTabSpec("Tab1");
View view = getLayoutInflater().inflate(R.layout.tab_indicator, myLayout, false);
tab1.setIndicator(view);
tab1.setContent(new Intent(getApplicationContext(), MyClass1.class));
tabHost.addTab(tab1);

In my custom view for indicator, there is a close button which is supposed to close that tab and the activity associated with it. Here is how I successfully removed the view from Tab-Widget when the close button is clicked on the tab.

tabHost.getTabWidget().removeViewAt(index_of_tab);

This code removes the tab, but it does not remove the activity that was started in the FrameLayout (tabcontent), and it results in showing this Activity again when opened different tab with different Acvitiy.

For example, once I opened a tab with activity MyClass1 and closed it. Again I opened a new tab with activity MyClass2, but the old activity MyClass1 appears again instead of MyClass2.

Am I doing it wrong ? If it is wrong what is the correct way to close the tab along with the activity associated with it ?

Please Help. Thanks.

Upvotes: 1

Views: 888

Answers (1)

nKn
nKn

Reputation: 13761

I had to use a TabHost in one of my projects and as far as the tab closing goes, I gave it up and did it the brute way, as seems that TabHost is a bit tricky when closing tabs.

So I ended up calling .clearAllTabs() and restoring just the tabs I wanted to keep. The whole thing is even worse when you realize that once you've set your TabHost.TabSpec object, you cannot retrieve it, there's not any getTabSpec() method over a tab or similar, so I finally ended up declaring an ArrayMap<String, TabHost.TabSpec> and each time I add a tab, I also save it in that Map.

This way each time I have to call clearAllTabs() on the TabHost, I just have to iterate over the map and call .addTab() on each tab I want to keep with the TabHost.TabSpec as parameter (and of course, also remove the one I want to clear from the map).

Upvotes: 2

Related Questions