Sto provando a visualizzare una mappa in una serie di schede, usando i frammenti. Il problema che sto riscontrando è l'attività della mappa scompare se l'utente naviga verso un altro frammento, quindi torna indietro. Come posso visualizzare MapActivity in frammenti in un tabhost?MapActivity in TabHost Frammento che scompare dopo l'interruttore di tabulazione
La base di questo da numerose domande intorno a come integrare un MapActivity con frammenti (vedi MapView in a Fragment (Honeycomb))
MainTabActivity ha un tabhost e usa FragmentManager per visualizzare Frammenti nelle schede. MapFragment ha un tabhost e lo usa per visualizzare un MapActivity.
MapFragment utilizza LocalActivityManager per propagare gli eventi del ciclo di vita all'attività contenuta.
Così MapFragment: il layout
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>
Il codice in MainTabActivity per modificare il contenuto della scheda:
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();
}
disposizione MainTabActivity (cancellato una serie di layout di estranei):
Ancora: il problema riscontrato è che l'attività della mappa scompare se l'utente passa a un altro frammento, quindi torna indietro. Come posso visualizzare MapActivity in frammenti in un tabhost?
Grazie mille.
Hai finito per risolvere questo problema? Sto correndo contro lo stesso problema ... – Rockmaninoff