Hi guys, I am creating a SDK using Koin as depende...
# android
r
Hi guys, I am creating a SDK using Koin as dependency injector, but I don’t know how to get the context from the android library without having to make use of the Application, I have tried with Context Isolation and it didn’t work for me. does anyone know how I can do it? I need the context to make a context.startActivity.
j
You can use ContentProvider for this 🙂 It will be injected through your manifest. Can then start Koin instance from that lateinit 🙂
Or make it even more clean, use androidx startup: https://proandroiddev.com/app-startup-under-the-hood-with-koin-library-example-380e188568ad startup using ContentProvider under the hood 🙂
plus1 1
r
Thank you very much, I will keep an eye on it
j
But I recommend using Context Isolation regardless if this is SDK, to isolate it from other Koin instances 🙂
You can use that and inject singleton instance from ContentProvider/androidx startup I think was easiest, but a lot of different patterns can use 🙂
p
Why not letting the client of your SDK provide the context?
You can set it as parameter of specific function and the client will provide one when calling your SDK function
j
That is quite bad I would say, avoid that. Because Context could then be Application, Activity, Service etc and have odd side effects 🙂
But yeah can do that, but warning 🙂
r
@Pablichjenkov because the SDK must be very host-client agnostic, avoid requesting parameters from the client and is being developed with the purpose of being used from Android, Flutter or React Native.
j
And yeah warning about using from Unity I can say, as that give you a very odd Activity/Application not behaves as expected 😄
p
Got you, you can also delegate the navigation entirely to the client side. Give them a callback and they perform the navigation. So you won't care about how platforms navigate or the type of context. In any case, just check for above comment, the context can be an Activity or not. When it is not an Activity you have to use the new task flag. Because nonActivity context cannot start Activities, unless you use this flag.
1
👆 1