How to execute any operations in an *Adapter or a *ViewHolder under the foreground activity's lifecyclescope?

Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
Search for a command to run...

Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
No comments yet. Be the first to comment.
In this series, I will write about Android App Development.
Android, ViewModel, Dependency Injection, Annotation, Dagger2/Hilt, MVVM, JetPack
A practical guide to choosing the right coroutine primitive for your Android/Kotlin projects

This guide explains what Channels are, when to use them, how to use them correctly, and when NOT to use them — in simple, professional language. 1. What problem do Channels solve? In Kotlin coroutines, you often have multiple coroutines running at t...

Learn how to build preview-safe ViewModels in Jetpack Compose using Hilt.

HandlerInterceptor vs Filter in Spring Boot: A Practical Guide with JNDI Injection Prevention

Simple In-Memory Caching: A Tiny Trick with Massive Impact

If get to know something new by reading my articles, don't forget to endorse me on LinkedIn
Yes, we can do that, EASILY!
By using: DI (Hilt) | FUN, right?
Let's check the snapshot::

We have an abstract class "ActivityAdapter" that takes "Context" as a parameter and contain an Single API "getCoroutineScope()" to get the Activity's LifecycleScope (Force Casting) as a callback.
We are receiving the "Context" in the "TestAdapter" with the help of "DI, THE SAVIOUR" :D and adapter is extended from "ActivityAdapter". Now we can access the LifecycleScope of foreground activity's in the *Adapter or *ViewHolder in runtime.
Register the "TestAdapter" in the any module which is bound to Activity lifecyle or annotated with "@InstallIn(ActivityComponent::class)". This module make sure that "TestAdapter" receive the foreground activity context as a parameter when client "@Inject" "TestAdapter" in an Activity or a Fragment.
Happy Coding...