我正在尝试使用片段在一组选项卡中显示地图.我遇到的问题是,如果用户导航到另一个片段然后返回,则地图活动会消失.如何在tabhost中以片段形式显示MapActivity?
其基础来自于如何将MapActivity与片段集成的众多问题(请参阅片段中的MapView(Honeycomb))
MainTabActivity有一个tabhost,并使用FragmentManager在选项卡中显示Fragments.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;
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
MainTabActivity中用于更改选项卡内容的代码:
private void updateTab(String oldId, String tabId) …Run Code Online (Sandbox Code Playgroud)