ContextProvider
Singleton provider that grants access to global Android Context, Application, and current Activity references.
ContextProvider is automatically initialized at application launch (via a android.content.ContentProvider). Once initialized, it tracks the active activity lifecycle without requiring manual context passing throughout your codebase.
Key Features
Zero Boilerplate: Automatically initialized on app startup; no manual setup in
Application.onCreate()is required.Global Context Access: Easily retrieve the application context or Application instance from anywhere (e.g., repositories, singletons, network clients).
Current Activity Tracking: Safely retrieve the currently resumed Activity (held via WeakReference to prevent memory leaks).
Java Interoperability: All properties are annotated with
@JvmStaticfor seamless access from Java.
Usage Example (Kotlin)
// Access global application context
val context = ContextProvider.applicationContext
val packageName = context.packageName
// Access global Application instance
val app = ContextProvider.application
// Access current foreground Activity (if available)
val currentActivity = ContextProvider.activity
if (currentActivity != null) {
// Perform Activity-dependent operations
}Usage Example (Java)
Context context = ContextProvider.getApplicationContext();
Activity activity = ContextProvider.getActivity();Properties
Returns the global Application instance.
Returns the global application Context.