Hi guys, What is the recommended way to use Kotli...
# android-architecture
b
Hi guys, What is the recommended way to use Kotlin interface delegation in Android classes? I use a Fragment and want to "delegate" some portion of logic in that fragment to separate class. This way, I can use Kotlin interface delegation to reduce code in main fragment. Basically,
Copy code
class SomeFragment(viewManager: SomeViewManager) : SomeViewInterface by viewManager {

}
Now I want to pass a
View
to
SomeViewManager
class. What is the best way to do it? Should I have a setter in
SomeViewManger?
i
It’s not possible to do that with classes that are managed by the framework: i.e. fragments / dialogfragments / activities / etc.
What you can do is to remove the fragment logic to another class and compose it instead.
And the fragment is the one that build this class. Fragments / Activities are composition roots to your classes
g
maybe with a FragmentFactory tho..
i
Only API 28+
g
aren't you talking about
AppComponentFactory
?
FragmentFactory
is from androidx and I can't see any restrictions.
b
@ghedeon @Ianmedeiros Thanks for the points! I didn't know about
FragmentFactory
! With that, can I inject
SomeViewManager
and have a
setter
to set the view once
onViewCreated
is called?
a
with
FragmentFactory
you can inject
SomeViewManager
i
FragmentFactory only works on devices with API28+
are you willing to set your minimum target as api 28 and have no user base?
hehe