My usecase is as follows:
I have to fetch some data from the web endpoint, and I have to add tabs to the TabHost
. Every tab has a listview
, which will further get populated with data from a different web service. So far, I have able to add tabs dynamically in the TabHost, and listview is also populated properly. Everything works fine on first run. Now, when I tap on an item from the listview, its detailed view is opened. Now, if I press back button, I come at the screen with tab view. At this point, the listview under the tab gets disappeared.
I am not getting why this is happening. I am using fragment along with FragmentStatePagerAdapter
in my code.
My code is as follows:
TagsLandingFragment This fragment will show TabView
Here I am fetching data from the webservice in onViewCreated
:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tabsPagerAdapter = new TabsPagerAdapter(getFragmentManager(),
mFragmentsList);
getTaggedListings(); //In this method I am fetching data from the server. Once I get the data, I call initialiseTabHost to display the tabs
}
private void initialiseTabHost(JSONArray activeTags, String currentTag) {
if(!mFragmentsList.isEmpty())
mFragmentsList.clear();
if (activeTags.length() != 0) {
for (int i = 0; i < activeTags.length(); i++) {
try {
TagListingsModel model = new TagListingsModel();
model.setmFragment(TagDataFragment.newInstance(activeTags
.getString(i)));
model.setTagName(activeTags.getString(i));
if (activeTags.getString(i).equalsIgnoreCase(currentTag)) {
model.setActive(true);
} else {
model.setActive(false);
}
mFragmentsList.add(model);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
mTabHost.setup();
int currentTab = 0;
for (int i = 0; i < mFragmentsList.size(); i++) {
if (mFragmentsList.get(i).isActive()) {
currentTab = i;
}
addTab(getActivity(), this.mTabHost,
this.mTabHost
.newTabSpec(mFragmentsList.get(i).getTagName()));
mTabWidget.getChildAt(i).setBackgroundResource(
R.drawable.selector_tab);
}
mTabHost.setOnTabChangedListener(this);
tabsPagerAdapter.notifyDataSetChanged();
// Fragments and ViewPager Initialization
mViewPager.setAdapter(tabsPagerAdapter);
mViewPager.setOnPageChangeListener(this);
this.mTabHost.setCurrentTab(currentTab);
mTabHost.setVisibility(View.VISIBLE);
}
TagDataFragment is common fragment whose instances are displayed under the tab.
Update 1
In onViewCreated
, I passed getChildFragmentManager
instead of getFragmentManager
to the tab pager adapter as follows:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tabsPagerAdapter = new TabsPagerAdapter(getChildFragmentManager(),
mFragmentsList);
}
The list now appears but, now the same item is appearing multiple times in listview.