AAC-ViewModel ݾ
• The ViewModel class is designed to store and manage UI-related data in a lifecycle
conscious way. The ViewModel class allows data to survive configuration changes
such as screen rotations.
• Android AAC ViewModel ചݶ ഥ द ؘఠܳ ਬೡ ࣻ ח ҳઑ۽ ٣ੋೞਵݴ,
Android Lifecycle onDestroy() ٘о زೠ.
https://developer.android.com/topic/libraries/architecture/viewmodel
Fragment.java
@NonNull
@Override
public ViewModelStore getViewModelStore() {
if (getContext() == null) {
throw new IllegalStateException("Can't access ViewModels from detached fragment");
}
if (mViewModelStore == null) {
mViewModelStore = new ViewModelStore();
}
return mViewModelStore;
}
Slide 30
Slide 30 text
FragmentActivity.java
@NonNull
@Override
public ViewModelStore getViewModelStore() {
if (getApplication() == null) {
throw new 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;
}
Slide 31
Slide 31 text
ViewModelStore.java
package androidx.lifecycle;
public class ViewModelStore {
private final HashMap mMap = new HashMap<>();
final void put(String key, ViewModel viewModel) {
ViewModel oldViewModel = mMap.put(key, viewModel);
if (oldViewModel != null) {
oldViewModel.onCleared();
}
}
final ViewModel get(String key) {
return mMap.get(key);
}
/**
* Clears internal storage and notifies ViewModels that they are no longer used.
*/
public final void clear() {
for (ViewModel vm : mMap.values()) {
vm.onCleared();
}
mMap.clear();
}
}
ViewModelFactory
Application ೞա݅ ਸ ҃
/**
* {@link Factory} which may create {@link AndroidViewModel} and
* {@link ViewModel}, which have an empty constructor.
*/
public static class AndroidViewModelFactory extends ViewModelProvider.NewInstanceFactory {
private static AndroidViewModelFactory sInstance;
/**
* Retrieve a singleton instance of AndroidViewModelFactory.
*
* @param application an application to pass in {@link AndroidViewModel}
* @return A valid {@link AndroidViewModelFactory}
*/
@NonNull
public static AndroidViewModelFactory getInstance(@NonNull Application application) {
if (sInstance == null) {
sInstance = new AndroidViewModelFactory(application);
}
return sInstance;
}
private Application mApplication;
/**
* Creates a {@code AndroidViewModelFactory}
*
* @param application an application to pass in {@link AndroidViewModel}
*/
public AndroidViewModelFactory(@NonNull Application application) {
mApplication = application;
Slide 41
Slide 41 text
ViewModelFactory
ࢤࢿо হח ҃
public static class NewInstanceFactory implements Factory {
@SuppressWarnings("ClassNewInstance")
@NonNull
@Override
public T create(@NonNull Class modelClass) {
//noinspection TryWithIdenticalCatches
try {
return modelClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Cannot create an instance of " + modelClass, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot create an instance of " + modelClass, e);
}
}
}
Slide 42
Slide 42 text
ViewModelFactory
ழझథ ࢤࢿо ݶ?
class SharedViewModelFactory(data data data) : ViewModelProvider.Factory {
override fun create(modelClass: Class): T {
return SharedViewModel(data, data, data) as T
}
}
ViewModelProviders.of(this,
SharedViewModelFactory(data, data, data)).get(SharedViewModel::class.java)
FragmentActivity.java
/**
* Called when the fragment is no longer in use. This is called
* after {@link #onStop()} and before {@link #onDetach()}.
*/
@CallSuper
public void onDestroy() {
mCalled = true;
FragmentActivity activity = getActivity();
boolean isChangingConfigurations = activity != null &&
activity.isChangingConfigurations();
if (mViewModelStore != null && !isChangingConfigurations) {
mViewModelStore.clear();
}
}
Slide 48
Slide 48 text
ViewModelStore.java
/**
* Clears internal storage and notifies ViewModels that they are no
longer used.
*/
public final void clear() {
for (ViewModel vm : mMap.values()) {
vm.onCleared();
}
mMap.clear();
}
Slide 49
Slide 49 text
ViewModel.java
public abstract class ViewModel {
/**
* This method will be called when this ViewModel is no longer used
and will be destroyed.
*
* It is useful when ViewModel observes some data and you need to
clear this subscription to
* prevent a leak of this ViewModel.
*/
@SuppressWarnings("WeakerAccess")
protected void onCleared() {
}
}