android的fragment使用中static静态fragment实例遇到的坑
2016-05-25 13:14
555 查看
android的fragment使用中static静态fragment实例遇到的坑
场景描述:
FragmentActivity + viewpager + fragment
问题描述:
FragmentActivity finish过后 由于static的类变量还存在 没有销毁 导致多次启动应用 fragment 多次运行
代码片段:
宿主片段:
TabFragmentAdapter tfa = new TabFragmentAdapter( getSupportFragmentManager(), this, new GezitechFragment[]{FragmentBookShelf_.newIntent(),FragmentBookShop_.newIntent()}, getResources().getStringArray(R.array.tab_arr) ) ;
pager.setOffscreenPageLimit(2);
pager.setAdapter( tfa );
// 定义 SlidingTabLayout
stl.setDistributeEvenly(true);
stl.setSelectedIndicatorColors(getResources().getColor(R.color.white));
stl.setBackgroundColor(getResources().getColor(R.color.c_title_bg));
stl.setCustomTabView(R.layout.read_tab_item_view, R.id.tv_tab_item_des);
stl.setViewPager( pager ); // 加载ViewPager
fragment 片段:
static FragmentBookShelf_ _this = null;
public static FragmentBookShelf_ newIntent() {
if (_this == null) {
_this = new FragmentBookShelf_();
}
return (FragmentBookShelf_) _this;
}
解决办法:
在fragment中 ondestroty 销毁时 将 _this的值赋值为NULL 即可解决 finish 没有销毁的情况。。。
void onDestroty(){
_this = null;_
super.onDestroty();
}
场景描述:
FragmentActivity + viewpager + fragment
问题描述:
FragmentActivity finish过后 由于static的类变量还存在 没有销毁 导致多次启动应用 fragment 多次运行
代码片段:
宿主片段:
TabFragmentAdapter tfa = new TabFragmentAdapter( getSupportFragmentManager(), this, new GezitechFragment[]{FragmentBookShelf_.newIntent(),FragmentBookShop_.newIntent()}, getResources().getStringArray(R.array.tab_arr) ) ;
pager.setOffscreenPageLimit(2);
pager.setAdapter( tfa );
// 定义 SlidingTabLayout
stl.setDistributeEvenly(true);
stl.setSelectedIndicatorColors(getResources().getColor(R.color.white));
stl.setBackgroundColor(getResources().getColor(R.color.c_title_bg));
stl.setCustomTabView(R.layout.read_tab_item_view, R.id.tv_tab_item_des);
stl.setViewPager( pager ); // 加载ViewPager
fragment 片段:
static FragmentBookShelf_ _this = null;
public static FragmentBookShelf_ newIntent() {
if (_this == null) {
_this = new FragmentBookShelf_();
}
return (FragmentBookShelf_) _this;
}
解决办法:
在fragment中 ondestroty 销毁时 将 _this的值赋值为NULL 即可解决 finish 没有销毁的情况。。。
void onDestroty(){
_this = null;_
super.onDestroty();
}
相关文章推荐
- android获取唯一标识符
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- Android下OpenCV学习之-opencv读图
- Android开发中拦截或屏蔽返回键
- android动画使用一
- android note1
- android 解决计算数值不准确的问题
- Android 键盘属性
- Android系统服务(二)
- Android Emulator could not allocate o 无法启动安卓自带虚拟机解决办法
- 使用LruCache缓存,轻松解决图片过多造成的OOM
- Visual Studio Emulator for Android 的安装与使用 感觉最干净好看的模拟器.
- AndroidStudio查看方法具体实现(Open Implementation)
- Android PopupWindow使用,轻松实现微博主界面(二)
- Android开发中常见的设计模式
- Android账号注册实现点击获取验证码倒计时效果
- android图片压缩上传系列-基础篇
- Android Support兼容包详解
- Android 使用第三方SDK—友盟实现分享功能
- Android Navigation TabBar控件实现多彩标签栏