2012-01-11 80 views
5

我試圖在一組選項卡中使用片段顯示地圖。 我遇到的問題是地圖活動消失,如果用戶導航到另一個片段,然後回來。 如何在tabhost中的片段中顯示MapActivity?TabHost中的MapActivity片段開關後消失

該算法的基礎是圍繞着如何與片段一MapActivity集成了許多問題(見MapView in a Fragment (Honeycomb)

MainTabActivity有tabhost並使用FragmentManager顯示在標籤片段。 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?

非常感謝。

+0

你最終解決了這個問題嗎?我遇到了同樣的問題... – Rockmaninoff 2012-03-09 16:39:19

回答

5

片段我工作圍繞這一問題作爲的矢量以下...

我有一個班級變量:

private View mMapViewContainer; 

我創建包含地圖如下的標籤(其中MyMapActivity是在標籤中顯示的MapActivity和mTabHost是TabHost)在我的片段的onViewCreated方法:

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

現在地圖被當用戶導航到另一片段上,然後回重新顯示。

+0

順便說一句,我用來創建我的mMapViewContainer的代碼應該在這個答案的評論線程中記入@ChristopherK:http://stackoverflow.com/a/8126287/447197 – tafty 2012-04-30 17:48:10

3

如果你在項目中使用ViewPager,呼籲setOffscreenPageLimit()它如下所示:

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

其中片段是你創建

+0

非常感謝!這讓我對HOURS感到困惑,我終於找到了解決方案,即在幾個開關後我的碎片消失的問題!非常感謝! – Ruuhkis 2012-08-14 16:19:07