https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
b

BMG

02/19/2020, 9:51 AM
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

Ianmedeiros

02/19/2020, 5:17 PM
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

ghedeon

02/19/2020, 6:24 PM
maybe with a FragmentFactory tho..
i

Ianmedeiros

02/19/2020, 6:44 PM
Only API 28+
g

ghedeon

02/20/2020, 7:09 AM
aren't you talking about
AppComponentFactory
?
FragmentFactory
is from androidx and I can't see any restrictions.
b

BMG

02/20/2020, 7:33 AM
@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

ahulyk

02/20/2020, 10:37 AM
with
FragmentFactory
you can inject
SomeViewManager
i

Ianmedeiros

02/20/2020, 2:05 PM
FragmentFactory only works on devices with API28+
are you willing to set your minimum target as api 28 and have no user base?
hehe
3 Views