MapActivity en TabHost Fragmento que desaparece después de cambiar la pestaña

Estoy tratando de mostrar un mapa en un conjunto de pestañas, utilizando fragmentos. El problema que tengo es que la actividad del mapa desaparezca si el usuario navega a otro fragmento y luego vuelve. ¿Cómo puedo mostrar una MapActivity en fragmentos en un tabhost?

La base de esto es de las numerosas preguntas sobre cómo integrar una MapActivity con fragmentos (ver MapView en un Fragmento (Honeycomb) )

MainTabActivity tiene un tabhost y utiliza FragmentManager para mostrar Fragmentos en las fichas. MapFragment tiene un tabhost y lo usa para mostrar una MapActivity.
MapFragment utiliza LocalActivityManager para propagar eventos de ciclo de vida a la actividad contenida.

Así que 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; } 

Diseño de 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> 

El código en MainTabActivity para cambiar el contenido de la pestaña:

 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 (eliminado un montón de diseño no relacionado):

 <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> 

Nuevamente – El problema que tengo es que la actividad del mapa desaparezca si el usuario navega a otro fragmento, luego vuelve. ¿Cómo puedo mostrar una MapActivity en fragmentos en un tabhost?

Muchas gracias.

He trabajado alrededor de este problema de la siguiente manera …

Tenía una variable de nivel de clase:

 private View mMapViewContainer; 

Creé la pestaña que contiene el mapa como sigue (donde MyMapActivity es la MapActivity que se muestra en la pestaña y mTabHost es la TabHost) en el método onViewCreated de mi Fragmento:

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

Y luego en el método onStop () fragmentos hice lo siguiente:

 @Override public void onStop() { super.onStop(); ((ViewGroup) mMapViewContainer.getParent()).removeView(mMapViewContainer); } 

Ahora el mapa se vuelve a mostrar cuando el usuario navega a otro fragmento y luego de nuevo.

Si utilizó ViewPager en su proyecto, llame a setOffscreenPageLimit() como se muestra a continuación:

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

Donde fragmentos es el Vector de fragmentos que has creado

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.