adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.5k stars 126 forks source link

Unable to parcelize parcelable objects #78

Closed lcsphantom closed 10 months ago

lcsphantom commented 2 years ago

In the documentation here we are explained to use @Parcelize and Parcelable however when doing so, the app throws an exception when you tap home, leave the app or open another Intent:

FATAL EXCEPTION: main Process: ca.redacted, PID: 10491 java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = ca....redacted) at android.os.Parcel.writeSerializable(Parcel.java:2125) at android.os.Parcel.writeValue(Parcel.java:1895) at android.os.Parcel.writeList(Parcel.java:1104) at android.os.Parcel.writeValue(Parcel.java:1844) at android.os.Parcel.writeList(Parcel.java:1104) at android.os.Parcel.writeValue(Parcel.java:1844) at android.os.Parcel.writeArrayMapInternal(Parcel.java:987) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1620) at android.os.Bundle.writeToParcel(Bundle.java:1303) at android.os.Parcel.writeBundle(Parcel.java:1056) at android.os.Parcel.writeValue(Parcel.java:1813) at android.os.Parcel.writeArrayMapInternal(Parcel.java:987) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1620) at android.os.Bundle.writeToParcel(Bundle.java:1303) at android.os.Parcel.writeBundle(Parcel.java:1056) at android.os.Parcel.writeValue(Parcel.java:1813) at android.os.Parcel.writeArrayMapInternal(Parcel.java:987) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1620) at android.os.Bundle.writeToParcel(Bundle.java:1303) at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4969) at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8645) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) Caused by: java.io.NotSerializableException: ca...redacted at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1240) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1604) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1565) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1488) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1234) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:354) at android.os.Parcel.writeSerializable(Parcel.java:2120) at android.os.Parcel.writeValue(Parcel.java:1895)  at android.os.Parcel.writeList(Parcel.java:1104)  at android.os.Parcel.writeValue(Parcel.java:1844)  at android.os.Parcel.writeList(Parcel.java:1104)  at android.os.Parcel.writeValue(Parcel.java:1844)  at android.os.Parcel.writeArrayMapInternal(Parcel.java:987)  at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1620)  at android.os.Bundle.writeToParcel(Bundle.java:1303)  at android.os.Parcel.writeBundle(Parcel.java:1056)  at android.os.Parcel.writeValue(Parcel.java:1813)  at android.os.Parcel.writeArrayMapInternal(Parcel.java:987)  at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1620)  at android.os.Bundle.writeToParcel(Bundle.java:1303)  at android.os.Parcel.writeBundle(Parcel.java:1056)  at android.os.Parcel.writeValue(Parcel.java:1813)  at android.os.Parcel.writeArrayMapInternal(Parcel.java:987)  at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1620)  at android.os.Bundle.writeToParcel(Bundle.java:1303)  at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4969)  at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)  at android.os.Handler.handleCallback(Handler.java:938)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:246)  at android.app.ActivityThread.main(ActivityThread.java:8645)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 

In fact the example given in the documentation doesn't work. Versions used:

val voyager_version = 1.0.0-rc02

    implementation "cafe.adriel.voyager:voyager-navigator:$voyager_version"
    implementation "cafe.adriel.voyager:voyager-androidx:$voyager_version"
    implementation "cafe.adriel.voyager:voyager-hilt:$voyager_version"

id org.jetbrains.kotlin.android version 1.7.0 apply false

  ext {
        compose_ui_version = 1.2.0-rc01
        compose_compiler_version = 1.2.0
   }

I did see this thread: #https://github.com/adrielcafe/voyager/issues/59 but even when using something like:

@Parcelize
data class NavParam(
    var firstName: String = "",
): Parcelable

data class NewScreen(
    val navParam: NavParam
) : Screen {
}

The app still crashes.

DjakaTechnology commented 2 years ago

I also encountered this, for now I add Serializable into the mix to workaround this issue

@Parcelize data class NavParam( var firstName: String = "", ): Parcelable, Serializable

arslan-charyyev-pc commented 1 year ago

Can confirm that this issue is still present in the latest version (1.0.0-rc03).

Take the example from documentation: https://voyager.adriel.cafe/state-restoration

// ✔️ DO
@Parcelize
data class Post(/*...*/) : Parcelable

data class ValidScreen(
    val userId: UUID, // Built-in serializable types
    val post: Post // Your own parcelable and serializable types
) : Screen {
    // ...
}

This "DO" example will actually lead to a crash whenever ValidScreen has to be parcelized, such as when activity goes into background. The solution by @DjakaTechnology does indeed mitigate the issue.

zakrodionov commented 1 year ago

@adrielcafe @DevSrSouza @Syer10 I tagged you guys, because you are the main maintainers of the code. Please see this issue🙏🙏🙏

Kashif-E commented 1 year ago

Facing this on 1.0.0-rc02

Kashif-E commented 1 year ago

if some one using it in a kmm app. you can do expect actual to get java.io.Serilizable

actual typealias CommonSerializable = java.io.Serializable

expect CommonSerializable

and then

data class SomeClass(val foo: String) : CommonSerializable
ghostmane0404 commented 1 year ago

1.0.0-rc04 and it is still present. @adrielcafe is there any new on this theme?

DevSrSouza commented 1 year ago

The documentation was misleading, we have updated it with more samples and Multiplatform notice. https://voyager.adriel.cafe/state-restoration

KapilYadav-dev commented 1 year ago

Any update on this guys ????

Kashif-E commented 1 year ago

@KapilYadav-dev i have updated my comment that solves this issue

KapilYadav-dev commented 1 year ago

@Kashif-E what about desktop and ios for their platform?

DevSrSouza commented 10 months ago

@KapilYadav-dev is just a plain interface in this case

actual interface CommonSerializable