参看文章:ViewPager中切换界面Fragment被销毁的问题
ViewPager的默认加载缓存当前Fragment前后相邻的两个Fragment.当滑动切换界面的时候,非相邻界面信息将被释放。
解决方案:
方法一:
在Fragment的onCreateView方法中做if判断 当mapLayout为空时 初始化时重新加载.
if (mapLayout == null) {//做判断
mapLayout = inflater.inflate(R.layout.fragmentcircle, null);
mapView = (MapView) mapLayout.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
init();
}
return mapLayout;
方法二:
设置ViewPager的Fragment缓存界面数
在ViewPager所在的Activity中设置setOffscreenPageLimit();
注:缓存界面太多会占用大量内存,页面少的情况下用.
// 设置ViewPager的缓存界面数 防止在页面切换时Fragment被销毁
// viewPager.setOffscreenPageLimit(1);
// 参数:int limit — 缓存当前界面每一侧的界面数
完.