zsoltk / compose-router

[DEPRECATED] Routing functionality for Jetpack Compose with back stack
Apache License 2.0
636 stars 36 forks source link

How do I save the state in a previous view? #60

Closed zunjae closed 3 years ago

zunjae commented 3 years ago

Assuming you have two destinations, A and B. A displays a long list. You click on an item in the list to navigate to B. Once in B you click on the back button. The A view is entirely recreated. The list is at the top again. I am unsure if this is by design or not.

sealed class Routing {
  object A: Routing()
  data class B(val slug: String) : Routing()
}
@Composable
fun AppStore(routing: Routing = Routing.A) {
  Router(routing) { backStack ->
    val onItemClicked: (String) = { slug ->
      backStack.push(Routing.B(slug))
    }
    when (val value = backStack.last()) {
      is Routing.A-> AController(onItemClicked)
      is Routing.B-> BController(value.slug)
    }
  }
}

I've already added a rememberLazyListState but this seems to be recreated entirely as well

I am using version 0.24.2

zunjae commented 3 years ago

I notice that the Pokedex sample app has the same behavior, very interesting.

I don't know a single app in existence with this unusual behavior. Why would you want to recreate a screen and lose all state? I've been developing for android for 6 years now and I've made apps used by over a million people and this is the first time I'm seeing this.

zunjae commented 3 years ago

Fixed by switching to another library

noe commented 3 years ago

Fixed by switching to another library

Which one?

zunjae commented 3 years ago

Fixed by switching to another library

Which one?

Any library will work. Just avoid this one as it's riddled with bugs and the developer seems to be dead

noe commented 3 years ago

Any library will work. Just avoid this one as it's riddled with bugs and the developer seems to be dead

For me, this library works well and without problems, but I like the functionality you mentioned. If there are other libraries that implement the same behavior, maybe the author or someone else can implement the same approach here. Can you point me to any specific jetpack compose library with that functionality?

zunjae commented 3 years ago

Can you point me to any specific jetpack compose library with that functionality?

It's built into Jetpack Compose called "state". It allows stateless widgets to remember its state. Every time I use this compose-router library it seems to "forget" the state and recreate the entire view from scratch. Once I removed this library, the state is remembered again.

zunjae commented 3 years ago

For me, this library works well and without problems

That seems to be factually incorrect. You can even try out the sample apps made by the same developer. You will see the same issue there where navigating back, the view and its (scroll) state is recreated entirely.

noe commented 3 years ago

We have different opinions, that's ok. Anyway, can you point me to any specific jetpack compose library with that functionality? I think that the standard compose navigation can do this with scoped view models but I am not sure, and it doesn't support animations, which are supported by compose-router when used in combination with compose-backstack. Therefore, I am genuinely interested in knowing other libraries that do support this.

zunjae commented 3 years ago

Sorry, I am not willing to discuss with someone who sees incorrect facts as "different opinion". Consider yourself blocked.