ViewModel's purpose is to retain data on life cycle events and configuration changes.
But for certain configuration changes, like locale change, I would like to get a fresh instance of viewModel. How can I force recreation?
class HomeActivity : AppCompatActivity() {
var viewModelFactory: ViewModelProvider.Factory
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// How to get new instance on locale change?
var viewModel = ViewModelProviders.of(this, viewModelFactory)
.get(HomeViewModel::class.java)
}
}
Answering my own question :)
I had the chance to speak with Yigit Boyar (lead for Architecture Components) on Google I/O 2019.
He confirmed it's intentionally not possible to force recreation on ViewModel. The right solution is to observe changes and act on them.