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,在使用之前需要先在项目中引入相应的库。

相关内容

热门资讯

程序员教你!hhpoker万能... 程序员教你!hhpoker万能辅助器(辅助挂)真是是有挂(有挂规律辅助插件)1、很好的工具软件,可以...
记者揭秘!wepoker有脚本... 记者揭秘!wepoker有脚本吗(辅助挂)本来真的有挂(有挂详细辅助工具)该软件可以轻松地帮助玩家将...
科普分享!hhpoker哪个俱... 科普分享!hhpoker哪个俱乐部靠谱(辅助挂)都是存在有挂(有挂方法辅助工具)1、上手简单,内置详...
必备科技!hhpoker的辅助... 必备科技!hhpoker的辅助是真的吗(辅助挂)确实是有挂(有挂神器辅助工具)是不是有人用挂微扑克w...
我来教教你!aapoker辅助... 我来教教你!aapoker辅助工具安全吗(辅助挂)好像存在有挂(有挂头条辅助脚本)1、游戏颠覆性的策...
科技分享!aapoker辅助器... 科技分享!aapoker辅助器是真的吗(辅助挂)原来存在有挂(有挂透视辅助器)运辅助工具,进入游戏界...
解密关于!pokemmo手机脚... 解密关于!pokemmo手机脚本辅助器(辅助挂)一贯有挂(有挂神器辅助攻略)1)辅助插件:进一步探索...
必备科技!wepoker怎么设... 必备科技!wepoker怎么设置盖牌(辅助挂)一直真的有挂(有挂方式辅助脚本)1)有没有挂:进一步探...
专业讨论!wepoker透视脚... 专业讨论!wepoker透视脚本下载(辅助挂)都是真的有挂(有挂规律辅助工具)1、每一步都需要思考,...
大神推荐!wepoker辅助器... 大神推荐!wepoker辅助器安装包(辅助挂)原来有挂(有挂方针辅助软件)是不是有人用挂微扑克wpk...