This question is basically a copy of "Java list of uninstantiated classes" but for Kotlin.
My use case is that I have a FragmentStatePagerAdapter
and I would like to pass it an ArrayList
of uninstantiated Fragment
s. I have an abstract base class TutorialSlideFragment
that defines a basic class for some slides in a tutorial. Then, I have 20+ Fragment
s that extend TutorialSlideFragment
to add some more functionality if needed. I would like to pass an ArrayList
of uninstantiated TutorialSlideFragment
so that the adapter can instantiate them in the right class when needed.
Currently, I have the following:
ArrayList<Class<TutorialSlideFragment>> = arrayListOf( TutorialSlideFragment::class.java )
This works, but let's not forget that TutorialSlideFragment
is abstract, so this is not what I want to do. I created a WelcomeSlideFragment
that extends TutorialSlideFragment
. If I do the following:
var tutorialSlides: ArrayList<Class<TutorialSlideFragment>> = arrayListOf(
WelcomeFragment::class.java
)
I get the following error:
Type inference failed. Expected type mismatch:
required:kotlin.collections.ArrayList<Class<TutorialSlideFragment>>
found: kotlin.collections.ArrayList<Class<WelcomeFragment>>
How can I have an ArrayList
on uninstantiated classes that extend a base class?
Specify your type as covariant (out
) so subclass types can be included:
var tutorialSlides: ArrayList<Class<out TutorialSlideFragment>> = arrayListOf(
WelcomeFragment::class.java
)
Or to make it easier to instantiate them later, you can specify a list of constructors:
val tutorialSlides: ArrayList<() -> TutorialSlideFragment> = arrayListOf(
::WelcomeFragment
)
and call the constructors like any other function:
val newFragment = tutorialSlides[0]()