public <T extends ViewModel> T get(@NonNull Class<T> modelClass){ String canonicalName = modelClass.getCanonicalName(); if (canonicalName == null) { thrownew IllegalArgumentException("Local and anonymous classes can not be ViewModels"); } return get(DEFAULT_KEY + ":" + canonicalName, modelClass); }
public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass){ ViewModel viewModel = mViewModelStore.get(key);
/** * Clears internal storage and notifies ViewModels that they are no longer used. */ publicfinalvoidclear(){ for (ViewModel vm : mMap.values()) { vm.clear(); } mMap.clear(); } }
public ViewModelStore getViewModelStore(){ if (getApplication() == null) { thrownew IllegalStateException("Your activity is not yet attached to the " + "Application instance. You can't request ViewModel before onCreate call."); } if (mViewModelStore == null) { NonConfigurationInstances nc = (NonConfigurationInstances) getLastNonConfigurationInstance(); if (nc != null) { // Restore the ViewModelStore from NonConfigurationInstances mViewModelStore = nc.viewModelStore; } if (mViewModelStore == null) { mViewModelStore = new ViewModelStore(); } } return mViewModelStore; }
// We're already stopped but we've been asked to retain. // Our fragments are taken care of but we need to mark the loaders for retention. // In order to do this correctly we need to restart the loaders first before // handing them off to the next activity. mFragments.doLoaderStart(); mFragments.doLoaderStop(true); ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
ViewModelStore viewModelStore = mViewModelStore; if (viewModelStore == null) { // No one called getViewModelStore(), so see if there was an existing // ViewModelStore from our last NonConfigurationInstance NonConfigurationInstances nc = (NonConfigurationInstances) getLastNonConfigurationInstance(); if (nc != null) { viewModelStore = nc.viewModelStore; } }