DMCApps / NavigationFragment

The purpose of this manager is to work with the ViewPager, Tabs and Navigation drawer to handle a single stack flow of fragments on the screen. It makes use of a main Fragment as a container and presents and hides fragments within it as children.
MIT License
28 stars 8 forks source link

ManagerConfig causes NotSerializableException #26

Closed jackcsk closed 8 years ago

jackcsk commented 8 years ago

In NavigationManagerFragment.onSaveInstanceState(bundle) method, the outState.putSerializable(KEY_MANAGER_CONFIG, mConfig); call throws RuntimeException with the cause of NotSerializableException Upon investigation, the exception was caused by serialization of INavigationFragment class members, as indicated in the cause:

Caused by java.io.NotSerializableException: com.class.name.RedactedFragment
       at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
       at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
       at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
       at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
       at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
       at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
       at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
       at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
       at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
       at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
       at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
       at android.os.Parcel.writeSerializable(Parcel.java:1389)
       at android.os.Parcel.writeValue(Parcel.java:1341)
       at android.os.Parcel.writeArrayMapInternal(Parcel.java:644)
       at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
       at android.os.Bundle.writeToParcel(Bundle.java:1034)
       at android.os.Parcel.writeBundle(Parcel.java:669)
       at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:144)
       at android.os.Parcel.writeTypedArray(Parcel.java:1197)
       at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:396)
       at android.os.Parcel.writeParcelable(Parcel.java:1363)
       at android.os.Parcel.writeValue(Parcel.java:1268)
       at android.os.Parcel.writeArrayMapInternal(Parcel.java:644)
       at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
       at android.os.Bundle.writeToParcel(Bundle.java:1034)
       at android.os.Parcel.writeBundle(Parcel.java:669)
       at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3091)
       at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3692)
       at android.os.Handler.handleCallback(Handler.java:815)
       at android.os.Handler.dispatchMessage(Handler.java:104)
       at android.os.Looper.loop(Looper.java:194)
       at android.app.ActivityThread.main(ActivityThread.java:5824)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1010)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)

As according to the documentation of ObjectOutputStream.writeObject(obj) method: http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html#writeObject(java.lang.Object)

Write the specified object to the ObjectOutputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are written. Default serialization for a class can be overridden using the writeObject and the readObject methods. Objects referenced by this object are written transitively so that a complete equivalent graph of objects can be reconstructed by an ObjectInputStream.

My suggestion is to add the keyword transient to the following lines:

    public INavigationFragment rootFragment;

    public INavigationFragment masterFragment;
    public INavigationFragment detailFragment;
DMCApps commented 8 years ago

Marked the properties transient as per your request. Makes sense considering they are nullified after use anyways so there is no need for them to be serialized. Thanks for the suggestion!