百度地图 Fragment之间切换黑屏现象解决方案
来源:互联网 发布:英美关系知乎 编辑:程序博客网 时间:2024/05/12 17:37
用过百度地图的人多很忧伤,各种bug, 已无力吐槽,最无语的可能就是会出现黑屏现象,比如一个Activity包含三个Fragment, 其中一个Fragment嵌套MapView使用,在切换这三个Fragment时会出现明显的黑屏,这个问题出现很久了,很早的SDK版本就存在,现在最新的SDK版本依然存在这个问题,解决方案如下:
问题分析:百度地图切换界面出现黑屏原因是地图退出释放内存时渲染出现bug导致,因为是百度地图本身内部bug,所以我们无法从本质上去修复这个bug, 但是我们bug的出现黑屏现象隐藏起来,即:将地图的释放过程放入后台进行,从而避免黑屏的出现。
1,在使用MapView的Fragment的onResume/onPause方法中手动调用设置view是否可见,具体如下:
@Override protected void OnPause(){ mMapView.setVisibility(View.INVISIBLE); mMapView.onPause(); super.onPause(); }进入页面时@Override protected void onResume(){ mMapView.setVisibility(View.VISIBLE); mMapView.onResume(); super.onResume(); }
2, Activity对应XML布局中单独使用一个View来替换嵌套有MapView的Fragment,需要显示该Fragment 时,设置该View为可见,不使用时设置为不可见。
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/mapFrameLayout" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> <FrameLayout android:id="@+id/otherFrameLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="invisible" > </FrameLayout> </FrameLayout>
3,从地图界面切换只其他界面时,手动隐藏地图界面,并在后台释放内存,完整代码如下。
import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.widget.FrameLayout;public class MainActivity extends FragmentActivity{FragmentManager manager;FrameLayout mapFrameLayout;FrameLayout otherFrameLayout;@Overrideprotected void onCreate(Bundle arg0) {super.onCreate(arg0);setContentView(R.layout.activity_main);mapFrameLayout = (FrameLayout)findViewById(R.id.mapFrameLayout);otherFrameLayout = (FrameLayout)findViewById(R.id.otherFrameLayout);BaiduMapFragment mBaiduMapFragment = new BaiduMapFragment();manager = getSupportFragmentManager();manager.beginTransaction().add(R.id.mapFrameLayout, mBaiduMapFragment, "map_fragment").commit();}public void toMapFragment(View v){mapFrameLayout.setVisibility(View.VISIBLE);otherFrameLayout.setVisibility(View.INVISIBLE);BaiduMapFragment mBaiduMapFragment = new BaiduMapFragment();manager.beginTransaction().replace(R.id.mapFrameLayout, mBaiduMapFragment, "map_fragment").commit();}public void toOtherFragment(View v){mapFrameLayout.setVisibility(View.INVISIBLE);otherFrameLayout.setVisibility(View.VISIBLE);OtherFragment otherFragment = new OtherFragment();FragmentTransaction mTransaction = manager.beginTransaction();mTransaction.replace(R.id.otherFrameLayout, otherFragment, "other");Fragment fragment = manager.findFragmentByTag("map_fragment");if(fragment != null){mTransaction.remove(fragment);//释放百度地图 Fragment}mTransaction.commit();}}
Demo工程下载地址:http://download.csdn.net/detail/easyer2012/9275677
3 1
- 百度地图 Fragment之间切换黑屏现象解决方案
- Fragment切换百度地图,黑屏
- 解决百度地图Fragment切换黑屏问题
- 解决百度地图Fragment切换黑屏问题
- 解决 百度地图+slidingmenu 切换Fragment闪黑屏问题
- 解决Fragment+百度地图会出现黑屏闪屏的现象
- fragment中百度地图切换黑屏以及与surfaceview的切换
- Fragment嵌套高德地图,切换黑屏,切换卡死退出解决方案
- 高德地图Fragment切换出现黑屏
- 百度地图,在fragment中嵌套使用MapView时,切换后出现黑屏
- 解决百度地图+slidingmenu 切换黑边 ,切换Fragment闪黑屏,64位手机侧滑地图错位
- 解决百度地图+slidingmenu 切换黑边 ,切换Fragment闪黑屏,64位手机侧滑地图错位
- SlidingMenu + Fragment百度地图黑边、黑屏
- 百度地图页面切换黑屏闪
- ios8UIWebView中嵌入百度地图黑屏解决方案
- android开发百度地图黑屏闪退现象解决办法
- fragment中有高德地图,点击按钮切换fragment,出现黑屏闪过。
- android 解决fragment中有百度高德等地图,切换出现闪一下黑屏的问题
- 用脚本批量执行redis命令
- 【数据库】MySQL乱码解决(存入数据乱码和查询输出乱码)
- 物联网架构演进和微信智能设备平台开发
- CArray类
- WinDbg入门教程
- 百度地图 Fragment之间切换黑屏现象解决方案
- python join 和 split
- ios ViewController 页面跳转
- HTML5,js与Android native通信
- Android性能优化建议 Performance Tips
- 一步一步教你实现Periscope点赞效果
- Docker build WordPress
- Hibernate事务与并发问题处理(乐观锁与悲观锁)
- Spring HTTP Invoker-远程调用模式