AppContext

object AppContext

Utility object that manages the application's Context across the app lifecycle.

The AppContext object allows for safe access to the application's global Context throughout the app. It ensures that the Context is initialized properly before being used by other components, preventing potential issues like null pointer exceptions or uninitialized access.

Usage:

  • Call setUp(context: Context) once, typically in the onCreate() method of the Application class, passing the application Context.

  • Access the application Context anywhere in the app using AppContext.get(), but only after setUp() has been called. If you attempt to call get() before setup, an exception will be thrown.

Throws

if the Context is accessed before initialization via setUp.

Properties

Link copied to clipboard
private lateinit var application: Application

application instance for the app, initialized via setUp() method

Functions

Link copied to clipboard
fun get(): Context

Provides the application-wide Context.

Link copied to clipboard
fun setUp(context: Context)

Initializes the AppContext with the provided Context.