When developing an Android project, we have many trouble with context. Therefore I suggest a solution to avoid to pass context through layers. This solution is registry pattern.
What is registry pattern ?
You can read about registry pattern in this link.
In summary, we create all instances of business classes, datasource classes, network classes when starting app. If you need to use those classed, you can get it from registry service.
How to use registry pattern in Android project
Step 1: Create your Application class extend Application class Android.
You can read detail in this link
Step 2: Create ServiceRegistry class.
Step 3: Create and registry all classes that you need.
- Create class, such as: PageDataSource class.
- Register PageDataSource in OnCreate method of MyApplication class
Step 4: Use these classes.
With this solution, you can avoid to pass context to classes, such as: datasource, business, network, … Moreover, you can use ApplicationContext, you can avoid error when using context of activity (error when activity is killed by OS, …). Moreover code will be more clean, and avoid to use static method (easy to write unit-test)