在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment:
public void switchContent(Fragment fragment) { if(mContent != fragment) { mContent = fragment; mFragmentMan.beginTransaction() .setCustomAnimations(android.R.anim.fade_in, R.anim.slide_out) .replace(R.id.content_frame, fragment) // 替换Fragment,实现切换 .addToBackStack(null) .commit(); } }
但是,这样会有一个问题:
每次切换的时候,Fragment都会重新实例化,重新加载一边数据,这样非常消耗性能和用户的数据流量。
就想,如何让多个Fragment彼此切换时不重新实例化?
翻看了Android官方Doc,和一些组件的源代码,发现,replace()这个方法只是在上一个Fragment不再需要时采用的简便方法。
正确的切换方式是add(),切换时hide(),add()另一个Fragment;再次切换时,只需hide()当前,show()另一个。
这样就能做到多个Fragment切换不重新实例化:
public void switchContent(Fragment from, Fragment to) { if (mContent != to) { mContent = to; FragmentTransaction transaction = mFragmentMan.beginTransaction().setCustomAnimations( android.R.anim.fade_in, R.anim.slide_out); if (!to.isAdded()) { // 先判断是否被add过 transaction.hide(from).add(R.id.content_frame, to).commit(); // 隐藏当前的fragment,add下一个到Activity中 } else { transaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个 } } }
————Edited 2015.2.7————-
问题一:保存UI与数据的内存消耗
上面所述为避免重新实例化而带来的“重新加载一边数据”、“消耗数据流量”,其实是这个Fragment不够“纯粹”。
Fragment应该分为UI Fragment
和Headless Fragment
。
前者是指一般的定义了UI的Fragment,后者则是无UI的Fragment,即在onCreateView()
中返回的是null
。将与UI处理无关的异步任务都可以放到后者中,而且一般地都会在onCreate()
中加上setRetainInstance(true)
,故而可以在横竖屏切换时不被重新创建和重复执行异步任务。
这样做了之后,便可以不用管UI Fragment
的重新创建与否了,因为数据和异步任务都在无UI的Fragment中,再通过Activity 的 FragmentManager 交互即可。
只需记得在Headless Fragment
销毁时将持有的数据清空、停止异步任务。
public class UIFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment, container, false); return view; } ... }