Android Module and KodeinGlobal
See original GitHub issueI tried to imitate the bindings of android module in my Kodein Global. For example:
bind() from ProviderBinding(erased()) { ctx().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager }
In order to avoid momory leak, I made it like this:
bind<ConnectivityManager >() with instance(this@MyApp.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager )
The code above didn’t work, I print the bindings out and this binding exists, I also try to null-check the instance from Kodein.global and it is not null but when I try something like:
Kodein.global.instance<ConnectivityManager >().activeNetworkInfo.isConnected
my app crash and stack trace said that I tried to call these function on a null reference.
Howerver, when I change the code to this and explicitly cast it at retrieve time, it’s ok:
bind<Any>("connectivity") with instance(this@MyApp.getSystemService(Context.CONNECTIVITY_SERVICE))
I don’t know if I’m doing anything wrong. I wonder why it become null just when I call a function on it and not when I check if it is null. Another question is that if it is null at binding time, why does it run without yielding any error until I retrieve the faulty binding?
P/S: Sorry if I said something stupid (I just started with android recently) or any typo (English’s not my native language). Hope someone help me with this or even make an android module exclusively for Kodein Global (of course, with some usage note to avoid leaking memory)
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Well, after trying again with a refreshed mind I found the problem. It’s a stupid mistake I didn’t realize the important difference between the changes of my code.
The code that did not work:
Working one:
The one that it should be:
There it is, I miss one null check at first and suppose I misuse Kodein but the one I misuse is Android’s API. Well, I should never code when feeling tired again.
Anyway, thanks for your support, and great library you made. It solves many of my problems and simplifies my code a bunch. 👍
Well, if
instance<ConnectivityManager>().activeNetworkInfo.isConnected
does not crash and yields false, that it cannot be an error in Kodein.bind<Any>()
is a really bad practice !