2

Let's suppose I have a basic ViewModel class:

@HiltViewModel
open class BaseViewModel @Inject constructor(
    private val auth: FirebaseAuth,
    private val firebaseCrashlytics: FirebaseCrashlytics
): ViewModel() {

}

Is there a way how to extend the BaseViewModel without passing the arguments to the constructor?

@HiltViewModel
class ChildViewModel @Inject constructor(
    private val firebaseAuth: FirebaseAuth,
    private val firebaseCrashlytics: FirebaseCrashlytics
) : BaseViewModel(firebaseAuth, firebaseCrashlytics) {

}

I found one related question but unfortunately without any answers here

Marian Pavel
  • 2,726
  • 8
  • 29
  • 65
  • No I don't think you, because a constructor is still a constructor, dagger hilt can't do anything in order to change this. But you can remove `@HiltViewModel` and `@Inject` from your base class and make it abstract. – Andrew Jun 28 '21 at 11:39
  • Just do Field Injection for `FirebaseAuth` and `FirebaseCrashlytics` in `BaseViewModel` instead of constructor injection. But remember there are downsides for Field Injection: https://stackoverflow.com/questions/39890849/what-exactly-is-field-injection-and-how-to-avoid-it – Dr.jacky May 23 '22 at 13:42

0 Answers0