AndroidStudio中在不同Fragment之间切换时,MapFragment没有被隐藏。
创始人
2024-08-21 09:30:10
0

可以通过在Fragment的生命周期方法中手动控制Map Fragment的显示和隐藏来解决此问题。具体的做法是,在每个Fragment的onCreateView()方法中通过View.findViewById()方法来获取Map Fragment的实例,然后在onResume()方法中显示Map Fragment,在onPause()方法中将其隐藏。

示例代码如下:

public class FragmentA extends Fragment {
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_a, container, false);
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);
        if (mapFragment != null) {
            mapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap map) {
                    googleMap = map;
                }
            });
        }
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (googleMap != null) {
            googleMap.setVisible(true);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (googleMap != null) {
            googleMap.setVisible(false);
        }
    }
}

注意:上述示例代码中使用了Google Maps API,在使用之前需要先在项目中引入相应的库。

相关内容

热门资讯

透视解谜!wepoker怎么挂... 透视解谜!wepoker怎么挂底牌,wepoker买脚本靠谱吗-其实是真的有辅助工具(哔哩哔哩)1、...
透视了解!德普之星怎么设置埋牌... 透视了解!德普之星怎么设置埋牌,德普之星辅助器app-原来真的是有辅助神器(哔哩哔哩)1、免费脚本咨...
透视详情!aa poker透视... 透视详情!aa poker透视软件,aapoker ai插件-本来存在有辅助攻略(哔哩哔哩)能透视中...
透视专业!wepoker挂,w... 透视专业!wepoker挂,wepoker怎么设置透视-确实真的有辅助神器(哔哩哔哩)运辅助工具,进...
透视普及!wepoker怎么设... 透视普及!wepoker怎么设置盖牌,wepoker怎么下载游戏-一直一直都是有辅助软件(哔哩哔哩)...
透视曝光!wepoker免费脚... 透视曝光!wepoker免费脚本咨询,wepoker轻量版辅助-都是一直都是有辅助方法(哔哩哔哩)1...
透视解密!wepoker有透视... 透视解密!wepoker有透视功能吗,htx矩阵wepoker辅助-确实真的有辅助脚本(哔哩哔哩)1...
透视有挂!wpk辅助软件,wp... 透视有挂!wpk辅助软件,wpk透视辅助靠谱吗-一直真的有辅助教程(哔哩哔哩)1、玩家可以在线上大神...
透视分享!aapoker怎么控... 透视分享!aapoker怎么控制牌,aapoker ai插件-切实有辅助方法(哔哩哔哩)1、免费脚本...
透视解迷!werplan外挂,... 透视解迷!werplan外挂,智星菠萝辅助-真是真的是有辅助技巧(哔哩哔哩)1、很好的工具软件,可以...