https://kotlinlang.org logo
#compose
Title
# compose
k

Kata

10/02/2023, 6:37 AM
Hey, I am trying to achieve the following but so far have no luck: I have compose inside my app but instead of using a ViewModel I am using Redux. When my screen closes I would like to clean up the state. But what is important that when the screen is still on the backstack it should not be cleaned up to prevent the state from being lost when navigating back. I know in a viewModel word I could use the
onCleared()
method of the viewModel. I would expect to have something like a
DisposableSavableEffect
acting like the
rememberSavable
that would only be called once the screen is opened and then disposed when it is removed from the backstack. Any ideas on how to solve this issue with compose?
t

Timo Drick

10/02/2023, 10:34 AM
I don't think that this is possible with the standard components. Than you have to write you own navigation stack. But in my opinion this is not difficult in Compose.
k

Kata

10/02/2023, 10:47 AM
So basically the only architecture that is supported by compose out of the box it viewModel?🫠
t

Timo Drick

10/02/2023, 10:49 AM
What you want i don't think that you can do this with viewModels. Because you do not have enough control over the lifecycle of them.
k

Kata

10/02/2023, 10:50 AM
the
init
and
onClear
from the viewModel would be enough, they are only destroyed once the view is not in the stack
t

Timo Drick

10/02/2023, 10:51 AM
Ok when this is really the case why you do not use them. But i think this is not correct.
You should only depend on the states which are stored in the navigation stack you use. And the lifecycle of other components should be more or less independ.
k

Kata

10/02/2023, 11:11 AM
I want to achieve the same thing as if you would create a viewModel and in the
init
you would make a network call. Now this wouldn't be called again on orientation change or on back navigation. And I am afraid compose is only set up for viewmodel architecture
t

Timo Drick

10/02/2023, 11:12 AM
Compose is just the UI framework. But you get some helper classes for viewmodels which makes it easy to use. But you can do what you want.
If you want to have a component that live as long as the related screen is in the navigation stack i think than you have to implement your own navigation stack. Because this is not supported by the standard components. But as i said in Compose this is really easy. At the end it is more or less just a data struckture.
k

Kata

10/02/2023, 11:57 AM
I feel there should be a better way than rewriting the whole navigation
t

Timo Drick

10/02/2023, 12:04 PM
It depends on what you really need. In my opinion the official compose navigation is very limited and can only be used for navigation of main screens of you app maybe.
Also maybe important viewModels only exists in Android so if you want to have KMM you need to use your own system anyways.
i

Ian Lake

10/02/2023, 1:35 PM
Timo is right, there is no signal for this. Here's the still open ticket for a "permanently destroyed effect": https://issuetracker.google.com/issues/177562901
k

Kata

10/05/2023, 2:28 PM
Nice that is a feature that would be super to have