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 @JvmStatic for 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

Link copied to clipboard

Returns the currently active (resumed) Activity, or null if no activity is currently resumed.

Link copied to clipboard

Returns the currently active (resumed) Activity as a Context, or null if no activity is currently resumed.

Link copied to clipboard

Returns the global Application instance.

Link copied to clipboard

Returns the global application Context.