How to get Application Context in your Library without any Initialise method?

Mudit Sen
1 min readJun 28, 2023

--

Let’s say you have created android library. It has great utility to all Android developers but it has hard dependency on Application Context. And before developers can start calling your methods they need to call some sort initialise or init method in their Application file and if they don’t your libraries functionality breaks.

You wanted to create a simple API which took some input from the libraries user and provides result in a Callback. But now you are dependant to ask for applications context or activities context to even begin your flow.

So how do we solve this problem, a simple Content Provider can help.

Just create a Dummy Content provider. If will receive a lifecycle callback onCreate when the App launches. Initialise your library there and now can be independent from create a init method for you users.

Also add this provider in your library’s AndroidManifest.xml

--

--

Mudit Sen
Mudit Sen

Written by Mudit Sen

Android || React Native || Spring

Responses (1)