原文 http://blog.csdn.net/jackrex/article/details/9885469
简介
平常使用的FragmentPagerAdapter和FragmentStatePagerAdapter来自android.support.v4.app包用来构建ViewPager。
FragmentPagerAdapter更多的用于少量界面的ViewPager,比如Tab。划过的fragment会保存在内存中,尽管已经划过。而FragmentStatePagerAdapter和ListView有点类似,会保存当前界面,以及下一个界面和上一个界面(如果有),最多保存3个,其他会被销毁掉。
要注意的是FragmentStatePagerAdapter可能不经意间会造成内存未正常回收,严重导致内存溢出,比如图片资源没有释放,资源引用问题。(之前碰到过EditTextt由于保存焦点导致Fragment未被释放,以至于内存溢出,设置editText.saveEanble(false)就可以解决此问题)。
FragmentPagerAdapter与FragmentStatePagerAdapter差异原因
FragmentPagerAdapter创建View的过程
@Override
public Object instantiateItem(ViewGroup container, int position) {
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
final long itemId = getItemId(position); //获取id
// Do we already have this fragment?
String name = makeFragmentName(container.getId(), itemId);
//生成fragment Tag,格式”android:switcher:” + viewId + “:” + id;也可以通过这个来获取一个Fragment的实例
Fragment fragment = mFragmentManager.findFragmentByTag(name);
//下面判断是否有该Fragment,如果有就直接Attach到Activity里面去,没有的话就通过getItem方法获取新的实例
if (fragment != null) {
if (DEBUG) Log.v(TAG, “Attaching item #” + itemId + “: f=” + fragment);
mCurTransaction.attach(fragment);
} else {
fragment = getItem(position);
if (DEBUG) Log.v(TAG, “Adding item #” + itemId + “: f=” + fragment);
mCurTransaction.add(container.getId(), fragment,
makeFragmentName(container.getId(), itemId));
}
if (fragment != mCurrentPrimaryItem) {
FragmentCompat.setMenuVisibility(fragment, false);
FragmentCompat.setUserVisibleHint(fragment, false);
}
return fragment;
}
FragmentPagerAdapter移去View的过程
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
if (DEBUG) Log.v(TAG, “Detaching item #” + getItemId(position) + “: f=” + object
+ ” v=” + ((Fragment)object).getView());
mCurTransaction.detach((Fragment)object); //把fragment从Activity中detach掉
}
由上面注释代码可知FragmentPagerAdapter创建时候把Fragment Attach到Activity,让Fragment不可见的时候Detach掉,这时候只要是创建的Fragment都不会被销毁掉,可以通过这个Tag找到–”android:switcher:” + viewId + “:” + id。(期中viewId是viewpager id,如R.id.viewpager,id是position)。
FragmentStatePagerAdapter创建View的过程
@Override
public Object instantiateItem(ViewGroup container, int position) {
// If we already have this item instantiated, there is nothing
// to do. This can happen when we are restoring the entire pager
// from its saved state, where the fragment manager has already
// taken care of restoring the fragments we previously had instantiated.
if (mFragments.size() > position) {
//mFragments是用来保存Fragment的ArrayList
Fragment f = mFragments.get(position);
if (f != null) {
return f;
}
}
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
Fragment fragment = getItem(position);
if (DEBUG) Log.v(TAG, “Adding item #” + position + “: f=” + fragment);
if (mSavedState.size() > position) {
Fragment.SavedState fss = mSavedState.get(position);
if (fss != null) {
fragment.setInitialSavedState(fss); //恢复Fragment的状态
}
}
while (mFragments.size() mFragments.add(null);
}
FragmentCompat.setMenuVisibility(fragment, false);
mFragments.set(position, fragment); //把新生成的fragment按照对应的position保存到mFragments里面去
mCurTransaction.add(container.getId(), fragment);//把fragment Attach到Activity
return fragment;
}
FragmentStatePagerAdapter销毁View的过程
public void destroyItem(ViewGroup container, int position, Object object) {
Fragment fragment = (Fragment)object;
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
if (DEBUG) Log.v(TAG, “Removing item #” + position + “: f=” + object
+ ” v=” + ((Fragment)object).getView());
while (mSavedState.size() mSavedState.add(null);
}
mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment)); //保存fragment的状态,如果有设置的话
mFragments.set(position, null);//清空mFragments列表中该位置的fragment
mCurTransaction.remove(fragment);//把fragment移除
}
由上面注释代码可知FragmentStatePagerAdapter创建时候把Fragment存到mFragments里面,并且Attach到Activity;销毁的时候从mFragments中移除,并且从Activity中移除。
关于如何获取FragmentStatePagerAdapter的实例引用,它不能通过”android:switcher:” + viewId + “:” + id来获取,可以使用instantiateItem(mViewPager,position)来获取,而FragmentPagerAdapter两种方法都可以。
欢迎指正。