问题
I had a nice a pretty ViewPager/FragmentStatePagerAdapter working on my app but after manually adding a Fragment to the FragmentManager I started getting this exception when rotating the screen TWICE.
StackTrace:
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
This is the code where I manually add the Fragment to the manager:
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();
}
来源:https://stackoverflow.com/questions/17935224/fragement-no-longer-exists-for-key-f0-index-1