eygraber
07/06/2018, 9:21 PMabstract class MyFragment: Fragment, KodeinAware {
private val parentKodein: Kodein by closestKodein()
override val kodein = Kodein {
}
// contrived example, but whatever
// doesn't work because activity is null
private val presenter: MyActivityPresenter by on(context = activity).instance()
// used to be able to do:
// private val presenter: MyActivityPresenter by with { activity }.instance()
}
streetsofboston
07/07/2018, 2:27 PMprivate val presenter : MyActivityPresenter by kodein.instance()
would work fine.
Be sure to do this as well (it may have some syntax errors, but you'll get the gist):
override val kodein = Kodein {
extend(parentKodein)
... you own stuff scoped to this fragment only, if any ...
}
eygraber
07/08/2018, 2:02 AMMyActivityPresenter
is scoped to the Activity?
Is it because it extends the parent Kodein (I forgot to copy extend(parentKodein)
in my example)?streetsofboston
07/09/2018, 12:41 PMclass MyActivity : AppComaptActivity(), KodeinAware {
...
override val kodein = Kodein {
val parentKodein by closestKodein()
extend(parentKodein)
bind<MyActivityPresenter>() with singleton { MyActivityPresenter(...) }
...
}
Then, if you don't plan to define on any dependencies in your Fragment, you can just access them as follows:
abstract class MyFragment : Fragment(), KodeinAware {
...
override val kodein = Kodein {
val parentKodein by closestKodein()
extend(parentKodein)
}
private val presenter : MyActivityPresenter by kodein.instance()
...
}
eygraber
07/09/2018, 6:59 PMon(context = activity)
.
The actual binding is:
bind<MyActivityPresenter>() with scoped(AndroidLifecycleScope<Activity>()).singleton { ... }
I messaged the channel with my solution earlier today (although it's not a great solution).streetsofboston
07/09/2018, 7:17 PMactivity
possibly can be any activity, not just the one that owns the Fragment. Yep, then your solution seems to do the trick. 🙂