我有一个漂亮的 ViewPager/FragmentStatePagerAdapter 在我的应用程序上工作,但是在手动将 Fragment 添加到 FragmentManager 之后,我在旋转屏幕 TWICE 时开始收到此异常。
堆栈跟踪:
07-29 19:10:22.360: E/FragmentManager(712): Fragement no longer exists for key f0: index 1
07-29 19:10:22.360: E/FragmentManager(712): Activity state:
07-29 19:10:22.407: D/FragmentManager(712): Active Fragments in 4608be98:
07-29 19:10:22.410: D/FragmentManager(712): #0: RESTFragment{46005fb0 #0 RESTFragment}
07-29 19:10:22.410: D/FragmentManager(712): mFragmentId=#0 mContainerId=#0 mTag=RESTFragment
07-29 19:10:22.410: D/FragmentManager(712): mState=5 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
07-29 19:10:22.410: D/FragmentManager(712): mAdded=true mRemoving=false mResumed=true mFromLayout=false mInLayout=false
07-29 19:10:22.410: D/FragmentManager(712): mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
07-29 19:10:22.419: D/FragmentManager(712): mRetainInstance=true mRetaining=false mUserVisibleHint=true
07-29 19:10:22.419: D/FragmentManager(712): mFragmentManager=FragmentManager{45fbe180 in MyActivity{46016180}}
07-29 19:10:22.419: D/FragmentManager(712): mActivity=com.argeniss.theguymob.frontend.MyActivity@46016180
07-29 19:10:22.419: D/FragmentManager(712): #1: null
07-29 19:10:22.419: D/FragmentManager(712): Added Fragments:
07-29 19:10:22.419: D/FragmentManager(712): #0: RESTFragment{46005fb0 #0 RESTFragment}
07-29 19:10:22.419: D/FragmentManager(712): #1: MyPageFragment{46024658}
07-29 19:10:22.419: D/FragmentManager(712): FragmentManager misc state:
07-29 19:10:22.419: D/FragmentManager(712): mActivity=null
07-29 19:10:22.419: D/FragmentManager(712): mContainer=null
07-29 19:10:22.430: D/FragmentManager(712): mCurState=0 mStateSaved=true mDestroyed=true
07-29 19:10:22.430: D/FragmentManager(712): mAvailIndices: [1]
07-29 19:10:22.430: E/com.argeniss.theguymob.frontend.MyActivity(712): Fragement no longer exists for key f0: index 1
这是我手动将片段添加到管理器的代码:
private void addFragmentToManager() {
String fragmentTag = RESTFragment.TAG;
FragmentManager fragmentManager = getSupportFragmentManager();
restFragment = (RESTFragment) fragmentManager.findFragmentByTag(fragmentTag);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (restFragment == null) {
restFragment = new RESTFragment();
fragmentTransaction.add(restFragment, fragmentTag);
}
// Adds the transaction to the fragment manager.
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
}