The situation is : I tried to put a viewpager with a fragmentAdapter inside of another fragment, which is inside of a activity. And I tested these code on my Nexus5(5.1.1), they worked like a charm .And so did on our QA's phones.
But this morning , when I was 'enjoying' my regular online-app-running-data reading work, I found this crash happened over 150 times and continuely growing. I tried every method I could get, including the nest-fragments crashes we usually easy to get, and after a few searching on Internet, I concluded this result: "It's Google's mistake."
[Reason]
In Google's Android Source Code, something wierd found like this:
It means before 5.0.1, everytime a fragement invokes its onDestory or onDetach Method, its mChildFramentManager has never been reset. WOW. And I guess that's the reason.
If you look at the implementation of Fragment, you'll see that when moving to the detached state, it'll reset its internal state. However, it doesn't reset mChildFragmentManager (this is a bug in the current version of the support library). This causes it to not reattach the child fragment manager when the Fragment is reattached, causing the exception you saw.
/** * Adds an action to the queue of pending actions. * * @param action the action to add * @param allowStateLoss whether to allow loss of state information * @throws IllegalStateException if the activity has been destroyed */publicvoidenqueueAction(Runnableaction,booleanallowStateLoss){if(!allowStateLoss){checkStateLoss();}synchronized(this){if(mDestroyed||mActivity==null){thrownewIllegalStateException("Activity has been destroyed");}if(mPendingActions==null){mPendingActions=newArrayList<Runnable>();}mPendingActions.add(action);if(mPendingActions.size()==1){mActivity.mHandler.removeCallbacks(mExecCommit);mActivity.mHandler.post(mExecCommit);}}}
[Fix]
the answer is simple, we need to reset its child fragmentManager manully on devices carrying systems below Android 5.0.1.Here is the code , inside the onDetach method:
Adding libraries with resources 방식은, 라이브러리 프로젝트를 생성하여 개발프로젝트에 라이브러리를 추가하는 방법. 컴파일 할때는 라이브러리 프로젝트의 소스를 참고하여 이상이 없었으나, 실행하기 위해 APK를 만들 때 라이브러리 프로젝트의 Bin 폴더에 있는 리소스를 가져와서 APK를 만드는데, 라이브러리 프로젝트를 한 번도 Build 안했다면 Bin폴더에 아무것도 존재하지 않아 실행시 라이브러리를 찾을 수 없어 위와 같은 에러가 발생.
<해결방법>
라이브러리 프로젝트를 Build하고, 개발프로젝트를 Clean & Build 하고 실행하니 위의 에러가 해결.