4

we can read lambda function are serialize by default (https://discuss.kotlinlang.org/t/are-closures-serializable/1620),
but I getting error:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.dialogfragment.Arguments)

my class Arguments:

class Arguments(val function: ()-> Unit) : Serializable

What is wrong with my lambda ?
(I got this error when Android need to kill my fragment because low of memory :) )

  • 1
    Android does not "kill" fragments due to low memory conditions. Android terminates processes due to low memory conditions. Regarding your `Serializable` problem, I would not expect attempting to write a lambda expression to disk to work, and I would not expect attempting to save a lambda expression in the saved instance state `Bundle` to work. – CommonsWare Oct 28 '19 at 23:38
  • but functional interfaces saved into Bundle works – san_francisco Oct 28 '19 at 23:59
  • The most likely problem is that your lambda contains a reference to a non-serializable object. – Alexey Romanov Oct 29 '19 at 07:40
  • @CommonsWare Actually serializing and putting lambda expression into `Fragment. arguments` works just fine. But when `onSaveInstanceState` is called app crashes which is abnormal. I mean how come it works when serializing/deserializing from/to `Fragment.arguments` but it crashes when `onSaveInstanceState` is called? – Farid Feb 08 '22 at 15:10
  • @Farid: "Actually serializing and putting lambda expression into Fragment. arguments works just fine" -- the rest of your comment illustrates why this is not the case. The saved instance state `Bundle` is to be passed across process boundaries, and you cannot pass code that way. – CommonsWare Feb 08 '22 at 15:14
  • @AlexeyRomanov I am quite sure that is the case but how is that even possible that everything works just fine when serializing/deserializing a lambda function to/from `Fragment.arguments` that has reference to a non-serializable object but when `onSaveInstanceState` is called it crashes. – Farid Feb 08 '22 at 15:14
  • @CommonsWare Interesting. One more thing, why (or how) is it possible to serialize a lambda function that has a reference to a non-serializable object. I mean why a code like this `Bundle().putSerializable("KEY", [LAMBDA WITH REFERENCE TO A NON-SERIALIZABLE OBJECT])` works? – Farid Feb 08 '22 at 15:18
  • @Farid: I do not know, sorry – CommonsWare Feb 08 '22 at 15:34

1 Answers1

0

If you want to serialize or parcel something you need to ensure every field is serializable or parcelable. In your case you are trying to serialize val function: ()-> Unit which is not serializable. Unfortunately, you can't make function serializable. You should rethink this argument class and pass something different. Probably you can get some parameter which leads to making a decision and then invoking a function in your fragment.

Sebastian Pakieła
  • 2,970
  • 1
  • 16
  • 24
  • Who says Kotlin lambdas are not serializable? https://discuss.kotlinlang.org/t/are-closures-serializable/1620 – Farid Feb 08 '22 at 15:19