Let me help to know what happens with this scenari...
# koin-contributors
p
Let me help to know what happens with this scenario, please. Everything will runs OK or dependencies won’t be found because
scope<ActivityProfile>
was unloaded by
ActivityProfile#2
?
Copy code
1 - A activity class to see users profile, like ActivityProfile
2 - ActivityProfile has a module tied to it and this activity load and unload this module
3 - Using the app, ActivityProfile is called twice with distinct parameters like userIDs (so it's a expected behaviour)
4 - ActivityProfile#1 is in navigation stack and ActivityProfile#2 is in top (user is looking for it)
5 - User destroy the activity using back button so ActivityProfile#2 calls onDestroy. When this activity is closed user start to look ActivityProfile#1
6 - ActivityProfile#1 try to access Koin dependencies and ??????
a
could you have a sample app to test it? this one is tricky 🤔
p
Hello @arnaud.giuliani, finally I got time to create this sample app. I explained in description and recorded a video to doesn’t need to checkout and run project. I got the answer, Koin throws a
NoBeanDefFoundException
.
a
Thanks for gathering all of this. I need to check that 👌
p
Great!
If you change my code to load module in startKoin (application level) and delete load/unload from activity, the crash don’t happen. So, the summary is when you’re using scope tied to activity but handling load/unload manually, then Koin it’s not handling with hash activity. I guess.
a
interesting 👍
p
@arnaud.giuliani do you think it can be fixed or is it a behaviour expected?
If it can be fixed, in my mind, we need to get the activity/fragment in load/unload. Some new extension function. With this information, probably call some method from Koin that generates a hash to activity version. Like when is used in Application level.
a
I have time tmr to check your case, I’ll ping you then 👍
p
I’m sorry, but what is
tmr
?
a
tomorrow 😄
2 comments for your case @Pedro Francisco de Sousa Neto • you can use factory without scope if needed • Why do you need to unload modules? you try to unload your modules after end of Activity, then first one is unloading everything on onDestroy.
• activityRetainedScope could help handle logic out of Activity logic ... but it’s still tricky
p
Is it not a good practice unload modules when activity is destroyed to decrease dependencies Koin map in memory?
Of course, the scenario is for the features who never can access Application level, like SDKs.
a
Is it not a good practice unload modules when activity is destroyed to decrease dependencies Koin map in memory?
is there any need for that? not sure. Apart if you have very large memory model loaded in memory.
my advise is to keep things simple: load modules when needed.
unloading module was intended to be a alternative to complex scope scenario. If you have overall your application that is bound to a “connected” session, it’s more easy to unload/reload on a new session that dealign with complex scope graph
p
Good arguments!