在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment:然后总感觉切换的时候有些卡顿,原来的代码

/**
* 切换页面,这里采用回调
*
* @param f
*/
public void switchFragment(Fragment f) {
if (f == null)
return;
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fl_main, f);
// transaction.addToBackStack(descString);
transaction.commit(); // 让menu回去
menu.toggle(); }

但是,这样会有一个问题:

每次切换的时候,Fragment都会重新实例化,重新加载一边数据,这样非常消耗性能和用户的数据流量。

就想,如何让多个Fragment彼此切换时不重新实例化?

翻看了Android官方Doc,和一些组件的源代码,发现,replace()这个方法只是在上一个Fragment不再需要时采用的简便方法。

正确的切换方式是add(),切换时hide(),add()另一个Fragment;再次切换时,只需hide()当前,show()另一个。

这样就能做到多个Fragment切换不重新实例化

/**
* 切换页面的重载,优化了fragment的切换
*
* @param f
* @param descString
*/
public void switchFragment(Fragment from, Fragment to) {
if (from == null || to == null)
return;
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction().setCustomAnimations(R.anim.tran_pre_in,
R.anim.tran_pre_out);
if (!to.isAdded()) {
// 隐藏当前的fragment,add下一个到Activity中
transaction.hide(from).add(R.id.fl_main, to).commit();
} else {
// 隐藏当前的fragment,显示下一个
transaction.hide(from).show(to).commit();
}
// 让menu回去
menu.toggle();
}

对Fragment切换的优化的更多相关文章

  1. ViewPager -- Fragment 切换卡顿 性能优化

    当ViewPager切换到当前的Fragment时,Fragment会加载布局并显示内容,如果用户这时快速切换ViewPager,即 Fragment需要加载UI内容,而又频繁地切换Fragment, ...

  2. ViewPager+RadioGroup实现标题栏切换,Fragment切换

    1.说明: 在使用RadioGroup做标题栏切换的时候,跟ViewPager的滑动有冲突,最后查看了源码+断点调试解决了一些碰到的问题,写一篇博客总结一下,有同样需求的朋友可以借鉴一下,自己以后有用 ...

  3. fragment切换刷新 及下拉刷新

    此工程较BaiduLocationXMLFragmentDB相比:1.滑动fragment自动刷新该fragment2.下拉刷新fragment,上拉暂未实现 a.fragment切换刷新 1 . 由 ...

  4. 实现Fragment 切换时不重新实例化

    以前实现Fragment的切换都是用replace方法实现 public void startFragmentAdd(Fragment fragment) { FragmentManager frag ...

  5. Fragment 切换问题

    public void switchContent(Fragment fragment) { if(mContent != fragment) { mContent = fragment; mFrag ...

  6. 让多个Fragment 切换时不重新实例化、FragmentTabHost切换Fragment时避免UI重新加载

    http://www.tuicool.com/articles/FJ7VBb FragmentTabHost切换Fragment时避免UI重新加载 不过,初次实现时发现有个缺陷,每次FragmentT ...

  7. 两层Fragment嵌套,外层Fragment切换时内层Fragment不显示内容

    尊重他人劳动成果,转载请说明出处:http://blog.csdn.net/bingospunky/article/details/46847269 需求 在搭界面有这么样一个需求:须要两层的Frag ...

  8. 巧妙实现缺角radiogroup控制多个fragment切换和滑动

    在android开发中,用一个radiogroup控制多个fragment切换是十分常见的需求.但是如果fragment是一个ListView,如何保证滑动的时候通过缺角可以看到下面的listview ...

  9. Android使得Fragment 切换时不重新实例化

    以前实现Fragment的切换都是用replace方法实现 public void startFragmentAdd(Fragment fragment) { FragmentManager frag ...

随机推荐

  1. idea 配置文件导出,导入

    俗话说的好,磨刀不误砍柴工.配置好自己的工具,这样撸码就会更爽. 来来来,傻瓜式配图开始. 点击后会出现有一个导出设置框默认为全部导出 点击...处 可设置导出的settings.jar包的位置 在新 ...

  2. Android SlideAndDragListView,一个可排序可滑动item的ListView

    SlideAndDragListView简介 SlideAndDragListView,可排序.可滑动item显示”菜单”的ListView. SlideAndDragListView(SDLV)继承 ...

  3. JS里的函数的call()与back()方法

    function cat(){} cat.prototype={ food:"fish", say: function(){ alert("I love "+t ...

  4. NSDate时间

    NSDate 使用 ios时间的秒数 取当前时间的秒数 NSTimeInterval time = [[NSDate date] timeIntervalSince1970]; long long i ...

  5. [Angular] Omit relative path by set up in tsconfig.json

    For example, inside you component you want to import a file from two up directory: import store from ...

  6. C语言中 / 得到的结果

  7. Learn from Architects of Buildings

     Learn from Architects of Buildings Keith Braithwaite Architecture is a social act and the material ...

  8. wireshark分析包中关于三次握手和四次终止标识

    转自: http://hi.baidu.com/hepeng597/item/5ba27e0b98bc8de3ff240de0 三次握手Three-way Handshake 一个虚拟连接的建立是通过 ...

  9. LA 3942 - Remember the Word 字典树+DP

    看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  10. 7 Best jQuery & JavaScript PDF Viewer plugin with examples

    In this Post we are providing best jQuery PDF viewer plugin & tutorial with examples.Due to popu ...