airbnb / mavericks

Mavericks: Android on Autopilot
https://airbnb.io/mavericks/
Apache License 2.0
5.85k stars 499 forks source link

Add support for restoring ViewModels that were initially created with a companion factory in a superclass #566

Closed allenchen1154 closed 3 years ago

allenchen1154 commented 3 years ago

Companion factories may be declared in any superclass of a given Mavericks ViewModel. Therefore, when recreating an instance of a ViewModel, we need to store the ViewModel class from the declaration site, instead of the resolved class. In cases where a companion factory was used, we can call the same one that was used to initially create the ViewModel. If a companion factory was not used, we can safely call the constructor of the original declaration site class as before.

In situations where the State class does not have a default constructor and must be provided via a companion factory, this behavior was leading to a crash. This adds a unit test to StateRestorationTest that covers one such scenario.

@BenSchwab