Custom Scopes & Subcomponents in DI

<p>So far, we&rsquo;ve experimented with the&nbsp;<code>@Singleton</code>&nbsp;scope, understanding its ability to create, retain, and reuse objects. However, there are scenarios that&nbsp;<code>@Singleton</code>&nbsp;can&#39;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&nbsp;<code>@Singleton</code>&nbsp;annotation isn&#39;t tailored for such specific requirements.</p> <p>Enter custom scopes. These powerful tools allow us to define scopes beyond what&rsquo;s provided by AndroidX, granting us the flexibility to manage object lifecycles as needed.</p> <p>To understand this concept deeply, let&rsquo;s build on top of our last example.</p> <p>Let&rsquo;s suppose that we&rsquo;d like our&nbsp;<code>NetworkLayer</code>&nbsp;to be created only once in our application scope, but the&nbsp;<code>ComputeLayer</code>&nbsp;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&rsquo;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&rsquo;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>