2012-01-11 6 views
5

フラグメントを使用してタブのセットにマップを表示しようとしています。 私が抱えている問題は、ユーザーが別のフラグメントに移動して戻った場合にマップのアクティビティが消えることです。 tabhost内のフラグメントでMapActivityを表示するにはどうすればよいですか?TabHostのMapActivityがタブ切り替え後に消える

これの根拠は、フラグメントとMapActivityを統合する方法の周りに数多くの質問からである(MapView in a Fragment (Honeycomb)を参照)

MainTabActivityはtabhostを持っており、タブでフラグメントを表示するためにFragmentManagerを使用しています。 MapFragmentにはタブホストがあり、これを使用して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

答えて

5

を作成した断片のベクターである私は、この問題を回避働い

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

を続いて...

私はクラスレベルの変数を持っていました:

private View mMapViewContainer; 

次のように私は私の断片の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); 
} 

ユーザーが別のフラグメントに移動してから戻ると、マップが再表示されます。

+0

ちなみに、mMapViewContainerの作成に使用したコードは、この回答のコメントスレッドで@ChristopherKに入金される必要があります:http://stackoverflow.com/a/8126287/447197 – tafty

3

プロジェクトでViewPagerを使用した場合、以下に示すようにそれのためにsetOffscreenPageLimit()を呼び出す:フラグメントはあなたが

+0

ありがとうございました!これはHOURSのために私を悩ましていました。私は最終的に、いくつかのスイッチの後に私のフラグメントが消えていた問題を解決しました!どうもありがとうございます! – Ruuhkis