Custom Scopes & Subcomponents in DI
<p>So far, we’ve experimented with the <code>@Singleton</code> scope, understanding its ability to create, retain, and reuse objects. However, there are scenarios that <code>@Singleton</code> can't address. For instance, what if we want an object to be freshly instantiated with every new activity creation, yet consistently reused across all its child fragments? The <code>@Singleton</code> annotation isn't tailored for such specific requirements.</p>
<p>Enter custom scopes. These powerful tools allow us to define scopes beyond what’s provided by AndroidX, granting us the flexibility to manage object lifecycles as needed.</p>
<p>To understand this concept deeply, let’s build on top of our last example.</p>
<p>Let’s suppose that we’d like our <code>NetworkLayer</code> to be created only once in our application scope, but the <code>ComputeLayer</code> should be recreated for each activity.</p>
<p>So, first of all, you might have already guessed that to create the NetworkLayer only once in the application scope, there’s not much that we need to do. As long as it has a singleton scope application it should be recreated only once.</p>
<p>So, let’s try to figure out how can we recreate our custom scope for ComputeLayer.</p>
<p><a href="https://medium.com/@zuhayr.codes/custom-scopes-subcomponents-in-di-day-11-efd75458ef51">Visit Now</a></p>