English 中文(简体)
在塔霍斯特支离破碎的地图
原标题:MapActivity in TabHost Fragment disappearing after tab switch

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?

其依据是围绕如何将地图方法与碎块混为一谈的许多问题(见)。 MapView in a Fragment (Honeycomb)

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.

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

地图集:

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

The Code in MainTabActative for changing the tab content:

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();
}

主要的TabActivity下岗(删除了一个与外界无关的布局):

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

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?

非常感谢。

最佳回答

我在此问题上做了如下工作。

我有一个等级变量:

private View mMapViewContainer;

我制作了一个表格,其中载有以下地图(MyMapactative is the Map actative in the tab and mTabHost is the TabHost),该表格采用了我的分裂方法:

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

在Stop(Stop)方法中,我做了以下工作:

@Override
public void onStop() {
    super.onStop();

    ((ViewGroup) mMapViewContainer.getParent()).removeView(mMapViewContainer);
}

现在,当用户浏览另一个碎片然后回去时,该地图正在重新绘制。

问题回答

如您在项目中使用<编码>ViewPager,请上setOff ScreenPage Limit(),详情如下:

this.mViewPager.setOff ScreenPageLimit(fragments.size());

where fragments is the Vector of fragments you ve created





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签