不能保留SupportMapFragment内片段 [英] Can't retain SupportMapFragment inside Fragment

查看:107
本文介绍了不能保留SupportMapFragment内片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用谷歌地图片段和其他一些片段抽屉式导航栏中的应用程序。我创建了 SupportMapFragment 编程并把它在我的自定义 fragment_map 布局图的容器中。

问题是我要加比标记2000+ 以从数据库中的地图,不想每次都重新填写他们设备得到了更多的旋转。我以为 setRetainInstance(真)将解决这个问题,但它的不工作 - 我在0获得一个全新的地图,而标志和摄像头上方向改变0位置。该示例应用程序工作正常且保留istance,但他们都使用FragmentActivity这不是我的情况。这里片段是什么问题?

GoogleMapsFragment.java:

 公共类GoogleMapsFragment扩展片段实现OnMa $ P $ {padyCallback私人SupportMapFragment mapFragment;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    查看rootView = inflater.inflate(R.layout.fragment_map,集装箱,FALSE);    FragmentManager FM = getChildFragmentManager();
    mapFragment =(SupportMapFragment)fm.findFragmentById(R.id.map_container);
    如果(mapFragment == NULL){
        mapFragment = SupportMapFragment.newInstance();
        mapFragment.getMapAsync(本);
        mapFragment.setRetainInstance(真);
        。fm.beginTransaction()加(R.id.map_cont,mapFragment).commit();
    }
    返回rootView;
}@覆盖
公共无效onMa pready(GoogleMap的GoogleMap的){
    googleMap.addMarker(。新的MarkerOptions()位置(经纬度新(5,5)));    //长code地图初始化和这里添加标记
    // ...
}
}


解决方案

在默认情况下,当配置更改,如方向,整个活动将重新启动,但用户可以通过指定这样做prevent它时,活动可以处理本身,这意味着你可以告诉活性,不重新启动,而是打电话onConfigurationChanged

阅读更多关于它在这里:
<一href=\"http://stackoverflow.com/questions/12263503/how-can-i-save-the-fragment-when-i-rotate-the-screen\">how我可以保存片段?当我旋转屏幕

I have an app with navigation drawer with Google Maps fragment and some other fragments. I'm creating the SupportMapFragment programmatically and putting it in my custom fragment_map layout in map container.

The problem is I have to add more than 2000+ markers to the map from database but do not want to fill them again every time the device got rotated. I thought the setRetainInstance(true) will solve the issue but it's not working — I'm getting a brand new map without markers and camera in 0, 0 position on orientation change. The sample apps are working fine with retained istance but they all are using FragmentActivity which is not my case. What is the problem with fragments here?

GoogleMapsFragment.java:

public class GoogleMapsFragment extends Fragment implements OnMapReadyCallback {

private SupportMapFragment mapFragment;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_map, container, false);

    FragmentManager fm = getChildFragmentManager();
    mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
    if (mapFragment == null) {
        mapFragment = SupportMapFragment.newInstance();
        mapFragment.getMapAsync(this);
        mapFragment.setRetainInstance(true);
        fm.beginTransaction().add(R.id.map_cont, mapFragment).commit();
    }
    return rootView;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    googleMap.addMarker(new MarkerOptions().position(new LatLng(5, 5)));

    // a long code with map initialization and adding markers here
    // ...
}
}

解决方案

By default when a configuration changes, such as the orientation, the entire activity is restarted, however you can prevent it from doing so by specifying when the activity can handle itself, this means you can tell the activity to not restart and instead call onConfigurationChanged

read more about it here : how can I save the fragment? when I rotate the screen

这篇关于不能保留SupportMapFragment内片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆