MapActivity in TabHost Fragment disappearing after tab switch

徘徊边缘 提交于 2019-12-03 08:03:55

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 tab
Intent intent = new Intent(getActivity(), MyMapActivity.class);
Window window = mLocalActivityManager.startActivity(MAP_ACTIVITY_TAG, intent);
mMapViewContainer = window.getDecorView();
mMapViewContainer.setVisibility(View.VISIBLE);
mMapViewContainer.setFocusableInTouchMode(true);
((ViewGroup) mMapViewContainer).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

View tab = getActivity().getLayoutInflater().inflate(R.layout.tab_background, null);
((TextView) tab.findViewById(R.id.tv_tab_title)).setText(getResources().getString(R.string.map));
TabSpec tabSpec = mTabHost.newTabSpec(MAP_TAB_TAG).setIndicator(tab).setContent(new TabContentFactory() {

    @Override
    public 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.

sergey

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!