Fragment嵌套高德地图,切换黑屏,切换卡死退出解决方案

在使用FragmentTabHost切换含有高德地图的Fragment时遇到了黑屏和卡死退出的问题。解决方案包括:1)将MapView替换为TextureMapView,并继承TextureSupportMapFragment;2)避免使用FragmentTabHost,改为首次加载所有Fragment,通过hide/show管理,减少onCreateView调用,降低内存占用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局:
- 首页Activity使用FragmentTabHost切换3个Fragment,Fragment1中嵌套2个Fragment,一个Fragment装了高德地图MapView,还有一个普通的Fragment;

问题1:
- 在首页从地图Fragment切换到别的Fragment时出现短暂黑屏

解决方案:
- 布局中用TextureMapView代替MapView,其他基本都一样。
- 装有地图的Fragment继承TextureSupportMapFragment(这个加不加好像没什么影响)

问题2:
- 在首页从地图Fragment切换到别的Fragment再切回来出现卡死退出

解决方案:
- 首页的Fragment不用FragmentTabHost切换,因为每次切换Fragment都会onCreateView,占用太多内存,导致奔溃;
- 在首页第一次加载所有的Fragment,切换的时候通过hide、show的方式来管理Fragment,这样就不会每次切换都调用onCreateView方法了;

参考代码:

private List<Fragment> fragmentList;

    private static final int INDEX_HOME_FRAGMENT = 0;
    private static final int INDEX_ORDER_FRAGMENT = 1;
    private static final int INDEX_MY_FRAGMENT = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        AppManager.getAppManager().addActivity(this);
        initUi();
        initFragments();
    }

    private void initFragments() {
        if (fragmentList == null) {
            fragmentList = new ArrayList<>();
            fragmentList.add(new HomeFragment());
            fragmentList.add(new OrderFragment());
            fragmentList.add(new MyFragment());

            if (fragmentList.size() == 3) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                for (int i = 0; i < fragmentList.size(); i++) {
                    Fragment fragment = fragmentList.get(i);
                    transaction.add(R.id.fl_home_fragment_container, fragment);
                    transaction.hide(fragment);
                }
                transaction.show(fragmentList.get(INDEX_HOME_FRAGMENT));
                transaction.commit();
            }
        }
    }

    protected void initUi() {
        RadioGroup homeTabs = (RadioGroup) findViewById(R.id.rg_home_tabs);
        homeTabs.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int checkFragment = INDEX_HOME_FRAGMENT;
        switch (checkedId) {
            /*首页*/
            case R.id.rb_home_tab_home:
                checkFragment = INDEX_HOME_FRAGMENT;
                break;
            /*订单*/
            case R.id.rb_home_tab_order:
                checkFragment = INDEX_ORDER_FRAGMENT;
                break;
            /*我的*/
            case R.id.rb_home_tab_me:
                checkFragment = INDEX_MY_FRAGMENT;
                break;
            default:
                break;
        }
        //切换Fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            if (checkFragment == i) {
                transaction.show(fragmentList.get(i));
            } else {
                transaction.hide(fragmentList.get(i));
            }
        }
        transaction.commit();
    }

相应布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.onetoo.www.onetoo.activity.HomeActivity"
    >

    <FrameLayout
        android:id="@+id/fl_home_fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        />
    <View
        android:id="@+id/divide"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/activity_group_radioGroup"
        android:background="@color/shenhui"/>

    <RadioGroup
        android:id="@+id/rg_home_tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >

        <RadioButton
            android:id="@+id/rb_home_tab_home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:button="@null"
            android:checked="true"
            android:drawableTop="@drawable/selector_home_btn"
            android:gravity="center"
            android:padding="6dp"
            android:text="首页"
            android:textColor="@color/selector_color_tab"
            android:textSize="@dimen/font_size_middle"
            />

        <RadioButton
            android:id="@+id/rb_home_tab_order"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:button="@null"
            android:drawableTop="@drawable/selector_chat_btn"
            android:gravity="center"
            android:text="订单"
            android:textColor="@color/selector_color_tab"
            android:textSize="@dimen/font_size_middle"
            />

        <RadioButton
            android:id="@+id/rb_home_tab_me"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:button="@null"
            android:drawableTop="@drawable/selector_my_btn"
            android:gravity="center"
            android:text="我的"
            android:textColor="@color/selector_color_tab"
            android:textSize="@dimen/font_size_middle"
            />
    </RadioGroup>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值