androidkotlinconstructorclass-constructors

Kotlin abstract class secondary constructor


I can't create the secondary constructor for this abstract class.

@SuppressLint("StaticFieldLeak")
abstract class BaseAndroidViewModel(
        application: Application,
        private val creditsGetByIdUseCase: CreditsGetByIdUseCase,
        private val videosGetByIdUseCase: VideosGetByIdUseCase,
        private val reviewGetByIdUseCase: ReviewGetByIdUseCase,
        private val addToFavoritesUseCase: AddToFavoritesUseCase,
        private val getFavoriteByIdUseCase: GetFavoriteByIdUseCase
) : AndroidViewModel(application) {

constructor(application: Application) : this(application) // There's a cycle in the delegation calls chain error

Solution

  • You can call secondary constructor like this -

    @SuppressLint("StaticFieldLeak") abstract class BaseAndroidViewModel : AndroidViewModel{
    constructor(application: Application,
                creditsGetByIdUseCase: CreditsGetByIdUseCase,
                videosGetByIdUseCase: VideosGetByIdUseCase,
                reviewGetByIdUseCase: ReviewGetByIdUseCase,
                addToFavoritesUseCase: AddToFavoritesUseCase,
                getFavoriteByIdUseCase: GetFavoriteByIdUseCase) : super(application)
    
    constructor(application: Application) : super(application) }