我想在一组选项卡中显示地图,用碎片。 我遇到的问题是,如果用户导航到另一个片段,然后再在地图的活动消失。 我怎样才能在一个tabhost显示片段一MapActivity?
I'm trying to display a map in a set of tabs, using fragments. The problem I'm having is the map activity disappears if the user navigates to another fragment, then back. How can I display a MapActivity in fragments in a tabhost?
其基础是围绕如何整合片段一个MapActivity的许多问题(见图形页面中的一个片段(蜂窝))
The basis for this is from the numerous questions around how to integrate a MapActivity with fragments (see MapView in a Fragment (Honeycomb))
MainTabActivity具有tabhost并使用FragmentManager显示在选项卡中的片段。 MapFragment具有tabhost并使用它来显示一个MapActivity。 MapFragment确实使用LocalActivityManager来propogate生命周期事件的所包含的活动。
MainTabActivity has a tabhost and uses FragmentManager to display Fragments in the tabs. MapFragment has a tabhost and uses it to display a MapActivity. MapFragment does use LocalActivityManager to propogate lifecycle events to the the contained activity.
所以MapFragment:
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布局:
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>
在code在MainTabActivity改变标签的内容:
The code in MainTabActivity for changing the tab contents:
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布局(删除了一堆不相关的布局):
MainTabActivity layout (deleted a bunch of unrelated layout):
<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?
Again - The problem I'm having is the map activity disappears if the user navigates to another fragment, then back. How can I display a MapActivity in fragments in a tabhost?
谢谢了。
我的工作解决此问题如下...
I worked around this issue as follows...
我有一个类级别的变量:
I had a class level variable:
private View mMapViewContainer;
我创建了一个包含地图,如下所示的选项卡(其中MyMapActivity在标签中显示的MapActivity和mTabHost是TabHost)在我的片段onViewCreated方式:
I created the tab containing the map as follows (where MyMapActivity is the MapActivity displayed in the tab and mTabHost is the TabHost) in the onViewCreated method of my Fragment:
// 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()方法,我做了以下内容:
And then in the fragments onStop() method I did the following:
@Override
public void onStop() {
super.onStop();
((ViewGroup) mMapViewContainer.getParent()).removeView(mMapViewContainer);
}
现在当用户导航到另一片段和重新显示在地图上,然后回
Now the map is re-displayed when the user navigates to another fragment and then back.