Comments (6)
Curiously, this is the one thing I've been wondering about ever since creating backstack.lookupService<T>()
, I specifically remember thinking about this exact scenario about 4 years ago, but it somehow just never came up.
What I do know is that this is also why I added lookupFromScope()
in case something goes wrong.
In fact, to my surprise, this is what the changelog says:
-Simple Stack 1.13.1 (2018-11-25)
...
ADDED: lookupFromScope() and canFindFromScope() methods that begin the lookup from the specified scope, instead of the active-most one. This is to allow safer look-ups when the same service tag is used in different scopes.
So apparently I knew about this 4.5 years ago, but somehow it never came up in my usecases.
Knowing that, the following works:
val backstack = LocalBackstack.current
val key = LocalComposeKey.current as FirstKey
val eventHandler = rememberService<ActionHandler>()
val commonSharedService = remember {
backstack.lookupFromScope<CommonSharedService>(
key.scopeTag,
CommonSharedService::class.java.name
)
}
Not exactly optimal, there's clearly no reified
helper function for it in simple-stack-extensions
at this time (hence the manual ::class.java.name
) because we never ended up running into this problem, but this is the theoreticaly fix for this, as you do look up the nearest service by that name, and scopes are only evicted when the state changes fully execute.
I'm not sure what to do about it as this is happening according to lookupService
's design, but the solution really is to either use lookupFromScope
or use a specific service tag rather than T::class.java.name
if there are multiple instances of the same class. 🤔
from simple-stack.
So, something like this would be a solution?
matejdro/simple-stack-compose-integration@fffbc52
Maybe documentation should emphasize lookupFromScope
over lookupService
, since there is this gotcha with lookupService
?
from simple-stack.
I've been using by lazy { backstack.lookup<T>() }
for so long, I'll need to think of a good plan for this one
from simple-stack.
I added a rememberServiceFrom()
although I feel like I will still need to add documentation and think a bit more about this.
This generally hasn't come up as I usually have 1 specific service type for screen so that you can utilize lookup across screens via implicit parents.
from simple-stack.
I think this is "resolved" by Zhuinden/simple-stack-extensions@9c7d00f and Zhuinden/simple-stack-extensions@5471b83 + reusing service name is not technically "disallowed", disallowing it would be a breaking change.
It could be hypothetically possible to add a "strict mode" where this case throws but not sure if that really helps.
from simple-stack.
Yes, this has been resolved. Sorry, I forgot to close it.
from simple-stack.
Related Issues (20)
- Question: What is the purpose of RegistrationViewModel inside ServiceProvider in extension-compose-example? HOT 4
- What is the proper way to add a key to the top even if it already exists in the backstack? HOT 2
- Publish to MavenCentral instead of just Jitpack.io HOT 2
- Unable to start activity ComponentInfo{com.xxxxxxxxxx.yyyyyy/com.xxxxxxxxxx.yyyyyy.activities.MainActivity}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment q2.v1: could not find Fragment constructor HOT 7
- How to 'lookup<>()' scoped class from an Android Service HOT 2
- Lazy initialization of ScopedServices HOT 2
- KMM support? HOT 2
- Backstack is not available in a child Fragment's onCreate after process death
- DialogFragment and BottomDialogFragment as part of navigation stack, question HOT 2
- Verify that the new `setParentServices()`'s `lookupFromScope(..., EXPLICIT)` behavior is what is expected HOT 1
- Updating the Compose example to Compose 1.5.0 requires AGP 8.1
- Should Simple Stack be used in new projects HOT 14
- Crash due to activity.onBackPressed() when removing fragment hosting a WebView in AndroidView {} + FADE HOT 15
- Backstack with espresso HOT 3
- The service does not exist in any accessible scopes HOT 11
- Up navigation examples are hard to find HOT 4
- Consider adding Backstack.services() method as in GlobalServices.services() HOT 4
- Extensions should support Fragment in-app predictive back animations
- How to write unit tests based on the readme example HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simple-stack.