MapActivity no fragmento TabHost desaparecendo após a troca de guias
Estou tentando exibir um mapa em um conjunto de guias, usando fragmentos. O problema que estou tendo é que a atividade do mapa desaparece se o usuário navegar para outro fragmento e depois voltar. Como posso exibir um MapActivity em fragmentos em um tabhos
A base para isso é das inúmeras perguntas sobre como integrar um MapActivity com fragmentos (consulteMapView em um fragmento (Honeycomb))
@MainTabActivity possui um tabhost e usa o FragmentManager para exibir Fragments nas guias. O MapFragment possui um tabhost e o utiliza para exibir uma MapActivit
MapFragment usa LocalActivityManager para propagar eventos do ciclo de vida para a atividade contid
So 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 layout:
<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>
O código em MainTabActivity para alterar o conteúdo da guia:
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();
}
ayout @MainTabActivity (excluiu vários layouts não relacionados):
<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>
Novamente - O problema que estou tendo é que a atividade do mapa desaparece se o usuário navegar para outro fragmento e depois voltar. Como posso exibir um MapActivity em fragmentos em um tabhos
Muito obrigado.