【问题标题】:MapActivity in TabHost Fragment disappearing after tab switchTabHost Fragment中的MapActivity在选项卡切换后消失
【发布时间】:2012-01-11 23:45:23
【问题描述】:

我正在尝试使用片段在一组选项卡中显示地图。 我遇到的问题是,如果用户导航到另一个片段,然后返回,地图活动就会消失。 如何在 tabhost 的片段中显示 MapActivity?

其基础来自关于如何将 MapActivity 与片段集成的众多问题(请参阅MapView in a Fragment (Honeycomb))

MainTabActivity 有一个 tabhost,并使用 FragmentManager 在选项卡中显示 Fragment。 MapFragment 有一个 tabhost 并使用它来显示 MapActivity。
MapFragment 确实使用 LocalActivityManager 将生命周期事件传播到包含的活动。

所以 MapFragment:

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.map_fragment, container, false);
        mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
        mTabHost.setup(getLocalActivityManager());
        Intent intent = new Intent(getActivity(), MapActivityWrapper.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//does nothing
        TabHost.TabSpec tab = mTabHost.newTabSpec("map")
                .setIndicator("map")
                .setContent(intent);
        mTabHost.addTab(tab);
        return view;
    }

MapFragment 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical">
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@android:id/tabhost"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
        <LinearLayout android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <TabWidget android:id="@android:id/tabs"
                    android:visibility="gone"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/>
            <FrameLayout android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"/>
        </LinearLayout>
    </TabHost>
</LinearLayout>

MainTabActivity 中更改标签内容的代码:

private void updateTab(String oldId, String tabId) {
    FragmentManager fm = getSupportFragmentManager();
    Fragment old = fm.findFragmentByTag(oldId);
    Fragment selected = fm.findFragmentByTag(tabId);
    FragmentTransaction ft = fm.beginTransaction();
    boolean added = false;
    if (selected == null) {
        selected = createFragment(tabId);
    } else {
        try {
            ft.remove(selected);
        } catch (Exception e) {
            ft.add(android.R.id.tabcontent, selected, tabId);
            added = true;
            //exception for removing an unadded fragment... why throw an exception?
        }
    }
    if (old != null) ft.remove(old);
    if (!added) ft.replace(android.R.id.tabcontent, selected, tabId);
    if (!creating && !backStackProcessing) ft.addToBackStack(null);//member vars
    ft.commit();
}

MainTabActivity 布局(删除了一堆不相关的布局):

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#EFEFEF">
    <RelativeLayout android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <FrameLayout android:id="@android:id/tabcontent"
                android:layout_gravity="top"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@color/app_default_bg"
                android:layout_above="@+id/ad_region"
                android:layout_below="@+id/quick_action_region">
            <FrameLayout android:id="@+id/tab1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"/>
            <FrameLayout android:id="@+id/tab2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"/>
            <FrameLayout android:id="@+id/map_tab"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"/>
            <FrameLayout android:id="@+id/tab3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"/>
        </FrameLayout>
        <TabWidget android:layout_width="fill_parent"
                   android:id="@android:id/tabs"
                   android:layout_height="wrap_content"
                   android:layout_alignParentBottom="true"
                   android:layout_alignParentLeft="true"/>
    </RelativeLayout>
</TabHost>

再次 - 我遇到的问题是,如果用户导航到另一个片段,然后返回,地图活动就会消失。 如何在 tabhost 的片段中显示 MapActivity?

非常感谢。

【问题讨论】:

  • 你最终解决了这个问题吗?我遇到了同样的问题...

标签: android android-fragments android-tabhost mapactivity


【解决方案1】:

我按如下方式解决了这个问题...

我有一个类级变量:

private View mMapViewContainer;

我在我的 Fragment 的 onViewCreated 方法中按如下方式创建了包含地图的选项卡(其中 MyMapActivity 是选项卡中显示的 MapActivity,mTabHost 是 TabHost):

// 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);

然后在片段 onStop() 方法中我做了以下操作:

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

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

现在,当用户导航到另一个片段然后返回时,地图会重新显示。

【讨论】:

【解决方案2】:

如果您在项目中使用了ViewPager,请调用setOffscreenPageLimit(),如下所示:

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

fragments 是您创建的片段的向量

【讨论】:

  • 非常感谢!这困扰了我好几个小时,我终于找到了一个解决方案,解决了我的片段在几次切换后消失的问题!非常感谢!
猜你喜欢
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 2011-12-21
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多