I have researched this example repo to understand how to implement Kodein in the Android app. But I was confused about the context questions in the activity case in the example.
Here the code:
abstract class BaseActivity<B : ViewDataBinding, VM : ViewModel>: AppCompatActivity(), KodeinAware {
protected lateinit var binding: B
lateinit var viewModel: VM
private val _parentKodein by closestKodein()
override val kodein: Kodein by retainedKodein {
extend(_parentKodein, copy = Copy.All)
bind<Activity>() with singleton { this@BaseActivity }
bind<Context>("ActivityContext") with singleton { this@BaseActivity }
import(activityModule)
}
}
Here we see that we add the context of activity to the singleton.
But also I see that we get Kodein
by retainedKodein
function.
So this Kodein module has immune to activity destroying on rotation.
But I do not understand about activity's context, because singleton holds the context.
So I ask. Does Kodein reattach the context of activity? Could you describe to me the reattach mechanism? Because this is not clear for me now. Thank you guys!:))
The answer is no way! Kodein can not reatach activity context to his instance. I have used leackcanary util and found activity leacks in sample repo, also created isuue