https://kotlinlang.org logo
#decompose
Title
# decompose
j

Jan

10/04/2023, 6:32 PM
Hi @Arkadii Ivanov is there a way to preserve a dialog during orientation change and execute the correct lambda?
I have a component with a slot-navigation
I have set persistent to true
My Dialog-Config looks like this:
Copy code
@Parcelize
    data class DialogConfig(
        val title: StringResource,
        val message: StringResource,
        val onConfirm: () -> Unit = {},
    ) : Parcelable
a

Arkadii Ivanov

10/04/2023, 6:37 PM
You should never have lambdas in your persistent configurations. Configurations are serialized, and you are basically leaking everything that is captured by the lambda.
j

Jan

10/04/2023, 6:37 PM
yes
a

Arkadii Ivanov

10/04/2023, 6:37 PM
Just supply the callback when you instantiate the dialog component
j

Jan

10/04/2023, 6:39 PM
so If I have two different dialogs for different usecase then I have to supply to different callbacks right?
a

Arkadii Ivanov

10/04/2023, 6:40 PM
Yep
j

Jan

10/04/2023, 6:40 PM
thats the only way ... only simple callbacks are working otherwise its leaking due to the lambda bound to a component
a

Arkadii Ivanov

10/04/2023, 6:41 PM
I don't get it. If you pass callbacks via constructor and don't put callbacks in configurations, there should be no leaks.
j

Jan

10/04/2023, 6:48 PM
hard to explain. I thought I could pass different actions via config to my dialog component, but that is obviously not working because If I do so my components are not cleaned up correctly. Aftern rotation I do have 2 components of the same type. Thats because I added an onConfirm lambda via config and the old component is not cleaned up
a

Arkadii Ivanov

10/04/2023, 6:49 PM
You can distinguish your cases when creating the dialog component, based on the configuration
You can have separate configuration classes for different cases
j

Jan

10/04/2023, 6:50 PM
yes, maybe thats the problem. I thought I could just have one global config and pass lambda-callbacks to it
a

Arkadii Ivanov

10/04/2023, 6:51 PM
Yeah. That's an incorrect approach 🙂
j

Jan

10/04/2023, 6:52 PM
ok, I will try with several configs. In my specific case a dialog config If I really want to delete something and a dialog config If I really want to cancel the current changes
a

Arkadii Ivanov

10/04/2023, 6:53 PM
You can also add an enum to the configuration, and use that for distinguishing. That would be another way.
j

Jan

10/04/2023, 6:53 PM
yep
thanks for help
👍 1
2 Views