Thursday, June 11, 2015

Never call setRetainInstance(true) for a Fragment backed by Loaders

Never call setRetainInstance(true) when there is a List inside the Fragment which is backed by a Loader.

Loaders handle configuration changes itself, it will conflict with the saved Fragment instances.

I have run into the following errors a couple of times. By putting breakpoints inside the Android FragmentManager class, I've figured out that it is caused by Loaders calling onLoadFinished(..) while the reference to the Activity is null.

I don't have time to look into the FragmentManager class to figure out what the exact reason is that the mActivity is null right now so I just point it out. Will look into it in the future.


Process: com.ericliudeveloper.sharedbillhelper, PID: 11784 java.lang.NullPointerException at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:950) at android.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:785) at android.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1086) at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:469) at android.content.Loader.deliverResult(Loader.java:144) at android.content.CursorLoader.deliverResult(CursorLoader.java:110) at android.content.CursorLoader.deliverResult(CursorLoader.java:43) at android.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:265) at android.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:92) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5748) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method)

No comments:

Post a Comment