This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public HolderFragment() { | |
setRetainInstance(true); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HolderFragment holderFragmentFor(Fragment parentFragment) { | |
FragmentManager fm = parentFragment.getChildFragmentManager(); | |
HolderFragment holder = findHolderFragment(fm); | |
if (holder != null) { | |
return holder; | |
} | |
holder = mNotCommittedFragmentHolders.get(parentFragment); | |
if (holder != null) { | |
return holder; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static ViewModelStore of(@NonNull Fragment fragment) { | |
if (fragment instanceof ViewModelStoreOwner) { | |
return ((ViewModelStoreOwner) fragment).getViewModelStore(); | |
} | |
return holderFragmentFor(fragment).getViewModelStore(); | |
} | |
... | |
public static HolderFragment holderFragmentFor(Fragment fragment) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static ViewModelProvider of(@NonNull Fragment fragment) { | |
AndroidViewModelFactory factory = AndroidViewModelFactory.getInstance(checkApplication(checkActivity(fragment))); | |
return new ViewModelProvider(ViewModelStores.of(fragment), factory); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public <T extends ViewModel> T get(@NonNull Class<T> modelClass) { | |
String canonicalName = modelClass.getCanonicalName(); | |
if (canonicalName == null) { | |
throw new 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { | |
try { | |
return modelClass.newInstance(); | |
} catch (InstantiationException e) { | |
... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@NonNull | |
@Override | |
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { | |
if (AndroidViewModel.class.isAssignableFrom(modelClass)) { | |
try { | |
return modelClass.getConstructor(Application.class).newInstance(mApplication); | |
} catch ... { | |
... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final void put(String key, ViewModel viewModel) { | |
ViewModel oldViewModel = mMap.get(key); | |
if (oldViewModel != null) { | |
oldViewModel.onCleared(); | |
} | |
mMap.put(key, viewModel); | |
} | |
final ViewModel get(String key) { | |
return mMap.get(key); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static ViewModelProvider of(@NonNull Fragment fragment) { | |
AndroidViewModelFactory factory = AndroidViewModelFactory.getInstance(checkApplication(checkActivity(fragment))); | |
return new ViewModelProvider(ViewModelStores.of(fragment), factory); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void onCreate(Bundle savedInstanceState) { | |
MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class); | |
model.getUsers().observe(this, users -> { | |
// update UI | |
}); | |
} |