Android Context Provider is a simple utility library that allows you to easily access the application context from anywhere in your app. This can be especially useful for situations where accessing the context in a static manner is needed.
-
🛠️ Uses 0 dependencies, making it lightweight and easy to integrate.
-
💪 Easy initialization with your application context.
-
🎮 Access the application context from any part of your code.
The library is available on Maven Central.
To import it into your project, add the following to your build.gradle.kts file:
implementation("io.github.kdroidfilter:androidcontextprovider:1.0.1")or in Groovy :
implementation 'io.github.kdroidfilter:androidcontextprovider:1.0.1'-
✨ Automatic Initialization The
ContextProvideris automatically initialized using aContentProvider, which means you do not need to explicitly initialize it in yourApplicationclass. This reduces boilerplate and ensures that theContextProvideris ready to use as soon as your app launches. -
🔎 Getting the Context
You can access the application context (not an activity context) from anywhere in your app like this:
val context = ContextProvider.getContext() // Use the context as needed
or in Java :
Context context = ContextProvider.getContext(); // Use the context as needed
- Automatic Initialization: The
ContextProvideris initialized automatically viaContentProvider, eliminating the need for manual setup. - Error Handling: If you try to get the context before it is initialized,
ContextProviderwill throw anIllegalStateException. However, with automatic initialization, this scenario is unlikely.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! If you find any issues or have improvements to suggest, feel free to open an issue or a pull request on GitHub.