Retaining an object during a configuration change
If restarting your activity requires that you recover large sets of data, re-establish a network connection, or perform other intensive operations, then a full restart due to a configuration change might be a slow user experience. Also, it might not be possible for you to completely restore your activity state with the Bundle that the system saves for you with theonSaveInstanceState() callback—it is not designed to carry large objects (such as bitmaps) and the data within it must be serialized then deserialized on the main thread, which can consume a lot of memory and make the configuration change slow. In such a situation, you can alleviate the burden of reinitializing part of your activity by using a ViewModelobject. ViewModels are preserved across configuration changes so they are the perfect place to keep your UI data without having to query them again.