Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

323
Visualizações
SavedStateViewModelFactory constructed with empty constructor

After updating navigation component and lifecycle viewmodel version to 2.5.0-alpha01 I have got following issue.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.atcc.aewp/com.atcc.aewp.activity.SplashActivity}: java.lang.UnsupportedOperationException: SavedStateViewModelFactory constructed with empty constructor supports only calls to create(modelClass: Class, extras: CreationExtras).

Exception throws when view model is loaded

open class BaseActivity : AppCompatActivity() {

    private val appLanguageViewModel: AppLanguageViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        appLanguageViewModel.result.observe(this) {
            ...
        }
    }
}

Here is my view model class

class AppLanguageViewModel : ViewModel() {

    private val languageLiveData = MutableLiveData<Event<ApplicationLanguage>>()

    val result: LiveData<Event<ApplicationLanguage>> = languageLiveData

    fun setLanguage(applicationLanguage: ApplicationLanguage) {
        if (LanguagePreferences.instance().setLanguage(applicationLanguage.name)) {
            languageLiveData.postValue(Event(applicationLanguage))
        }
    }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Workaround;

    private val viewModel: AppLanguageViewModel by viewModels {
      SavedStateViewModelFactory(application, this)
    }

Reason;

Because not everything uses the new API of ViewModelFactory, we get this crash. The default factory in ComponentActivity is SavedStateViewModelFactory without any constructor parameter.

We have an issue with HiltViewModelFactory. It calls create method without passing new CreationExtras values. I assume this should be fixed in hilt in our case. If you can share the rest of the stack trace, we might find out the root cause in your case too.

over 4 years ago · Santiago Trujillo Relatório

0

In our case Sinan Kozak work around did not work. The current issues we were facing where because we were using the following versions of navigation and lifecycle

lifecycle

implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'

navigation

implementation 'androidx.navigation:navigation-fragment-ktx:2.5.0-alpha01'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.0-alpha01'

This resulted in Biometrics on the phone not working. So, the solution our end was to revert to the previous stable release of the navigation dependencies

implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'

I guess the best rule of thumb is do not use alpha build dependencies in production

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda