Zhuinden / simple-stack

[ACTIVE] Simple Stack, a backstack library / navigation framework for simpler navigation and state management (for fragments, views, or whatevers).
Apache License 2.0
1.36k stars 76 forks source link

Q: BackstackHost destroyed when activity is destroyed? #242

Closed valeriyo closed 3 years ago

valeriyo commented 3 years ago

I have turned on the "Don't keep activities" toggle in developer options to test state persistence.

As soon as I switch away from my app, the BackstackHost.onDestroy is called, which wipes out all scoped service instances as well. Is this how it's supposed to work?

I thought the scoped services should survive activity going away. But if BackstackHost has same lifecycle as the activities - then, what's the point of it?

Zhuinden commented 3 years ago

"Don't keep activities" is a lifecycle scenario that only happens when "don't keep activities" is enabled. BackstackHost is a retained fragment, so it has the same lifecycle as an Activity, but it is kept alive across configuration changes.

It appears that with "don't keep activities", all activities are immediately finish()ed when they are not in front, so that would mean that any other solution for surviving configuration changes, even Jetpack ViewModel's ViewModelStore, should be similarly terminated as you navigate between Activities if "don't keep activities" is enabled.

That would mean that only statics would survive, but statics aren't safe as they are shared between tasks.

Zhuinden commented 3 years ago

By standard Android behavior, finish() for Activity only happens when super.onBackPressed() happens.

valeriyo commented 3 years ago

Yeah, makes sense:

Zhuinden commented 3 years ago

I believe this is effectively answered, but "don't keep activities" is very interesting nonetheless