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.37k stars 75 forks source link

How to 'lookup<>()' scoped class from an Android Service #270

Closed uvaysss closed 1 year ago

uvaysss commented 1 year ago

Maybe I've missed it somewhere in samples, but I cannot find any examples how to lookup an object from the Android Service.

The goal is to share data and observe it from a Repository'ish like object in a Fragment and Android Service.

I'm currently using a dagger singleton for this, but was thinking migrate to simple stack scoped services incrementally.

Zhuinden commented 1 year ago

Hey hey, Backstack generally exists within an Activity, so the only viable scope accessible from a Service is that of GlobalServices.

For that, you'd need val globalServices = (context.applicationContext as YourApplication).globalServices to get it, and then it's globalServices.get<T>().

So it's same idea as here: https://github.com/Zhuinden/simple-stack/blob/8d37157aee38ad54d3471b6e064ce9bb37ab86a1/samples/advanced-samples/extensions-example/src/main/java/com/zhuinden/simplestackextensionsample/app/MainActivity.kt#L29-L33

uvaysss commented 1 year ago

That makes sense.. Did miss that part about GlobalServices. Thank you for your quick response!