我在 Android 应用程序中遇到了一个非常难以诊断的问题。
getUserVisibleHint()
在 false
中当前选定的片段上返回 ViewPager
,而它应该返回 true(因为它是可见且被选中的)。
我对看到此行为的实例进行了如下描述:
ViewPager
FragmentStatePagerAdapter
PagerAdapter
恢复
调试显示
FragmentStatePagerAdapter
实际上在 setPrimaryItem(ViewGroup container, int position, Object object)
中正确设置了所选选项卡的状态,但后来在 FragmentManager#moveToState(Fragment f, int newState, int transit, int transitionStyle, boolean keepActive)
中设置为 false
//from FragmentManager#moveToState(Fragment f, int newState, int transit, int transitionStyle, boolean keepActive)
f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(
FragmentManagerImpl.USER_VISIBLE_HINT_TAG, true);
上面的f.mSavedFragmentState
已将可见状态保存为false
,因为它是在片段不再在屏幕上时保存的。
所以这里的问题是状态丢失;可见状态在
FragmentStatePagerAdapter#setPrimaryItem
中设置,但在调用片段的 onResume
方法之前一段时间丢失。
在库中修复此错误之前,请覆盖
setPrimaryItem
中的 PagerAdapter
并强制首先提交所有待处理事务。
public static class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
//Force any pending transactions to save before we set an item as primary
finishUpdate(null);
super.setPrimaryItem(container, position, object);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
return "Page " + (position + 1);
}
}
要解决此问题,
FragmentStatePagerAdapter
必须在设置用户可见提示之前提交任何片段事务。
FragmentStatePagerAdapter
只是为了展示里面发生了什么
FragmentStatePagerAdapter
@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) {
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);
}
}
while (mFragments.size() <= position) {
mFragments.add(null);
}
fragment.setMenuVisibility(false);
fragment.setUserVisibleHint(false);
mFragments.set(position, fragment);
mCurTransaction.add(container.getId(), fragment);
return fragment;
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
Fragment fragment = (Fragment)object;
if (fragment != mCurrentPrimaryItem) {
if (mCurrentPrimaryItem != null) {
mCurrentPrimaryItem.setMenuVisibility(false);
mCurrentPrimaryItem.setUserVisibleHint(false);
}
if (fragment != null) {
fragment.setMenuVisibility(true);
fragment.setUserVisibleHint(true);
}
mCurrentPrimaryItem = fragment;
}
}
@Override
public void finishUpdate(ViewGroup container) {
if (mCurTransaction != null) {
mCurTransaction.commitNowAllowingStateLoss();
mCurTransaction = null;
}
}