Skip to content Skip to sidebar Skip to footer

Mapactivity In Tabhost Fragment Disappearing After Tab Switch

I'm trying to display a map in a set of tabs, using fragments. The problem I'm having is the map activity disappears if the user navigates to another fragment, then back. How can I

Solution 1:

I worked around this issue as follows...

I had a class level variable:

private View mMapViewContainer;

I created the tab containing the map as follows (where MyMapActivity is the MapActivity displayed in the tab and mTabHost is the TabHost) in the onViewCreated method of my Fragment:

// Map tabIntentintent=newIntent(getActivity(), MyMapActivity.class);
Windowwindow= mLocalActivityManager.startActivity(MAP_ACTIVITY_TAG, intent);
mMapViewContainer = window.getDecorView();
mMapViewContainer.setVisibility(View.VISIBLE);
mMapViewContainer.setFocusableInTouchMode(true);
((ViewGroup) mMapViewContainer).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

Viewtab= getActivity().getLayoutInflater().inflate(R.layout.tab_background, null);
((TextView) tab.findViewById(R.id.tv_tab_title)).setText(getResources().getString(R.string.map));
TabSpectabSpec= mTabHost.newTabSpec(MAP_TAB_TAG).setIndicator(tab).setContent(newTabContentFactory() {

    @Overridepublic View createTabContent(String tag) {
        return mMapViewContainer;
    }
});
mTabHost.addTab(tabSpec);

And then in the fragments onStop() method I did the following:

@Override
public void onStop() {
    super.onStop();

    ((ViewGroup) mMapViewContainer.getParent()).removeView(mMapViewContainer);
}

Now the map is re-displayed when the user navigates to another fragment and then back.

Solution 2:

If you used ViewPager in your project, call setOffscreenPageLimit() for it like shown below :

this.mViewPager.setOffscreenPageLimit(fragments.size());

where fragments is the Vector of fragments you've created

Post a Comment for "Mapactivity In Tabhost Fragment Disappearing After Tab Switch"