This file contains 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 сlass HolderFragment extends Fragment { | |
// Other code | |
private ViewModelStore mViewModelStore = new ViewModelStore(); | |
public HolderFragment() { | |
setRetainInstance(true); | |
} |
This file contains 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 abstract class ViewModel { | |
/** | |
Long comment | |
*/ | |
@SuppressWarnings("WeakerAccess") | |
protected void onCleared() { | |
} | |
} |
This file contains 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 class ViewModelProviders { | |
public static ViewModelProvider of(// arguments) { | |
// Code | |
return new ViewModelProvider(ViewModelStores.of(fragment), factory); | |
} | |
public static ViewModelProvider of(// arguments) { | |
// Code | |
return new ViewModelProvider(ViewModelStores.of(activity), factory); |
This file contains 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 class ViewModelProvider { | |
DEFAULT_KEY = "android.arch.lifecycle.ViewModelProvider.DefaultKey"; | |
// Code | |
public <T extends ViewModel> T get(Class<T> modelClass) { | |
String canonicalName = modelClass.getCanonicalName(); | |
// Code |
This file contains 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 String key, @NonNull Class<T> modelClass) { | |
ViewModel viewModel = mViewModelStore.get(key); | |
if (modelClass.isInstance(viewModel)) { | |
return (T) viewModel; | |
} else { | |
// Сode | |
} | |
viewModel = mFactory.create(modelClass); |
This file contains 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 сlass ViewModelStores { | |
public static ViewModelStore of(FragmentActivity activity) { | |
if (activity instanceof ViewModelStoreOwner) { | |
return ((ViewModelStoreOwner) activity).getViewModelStore(); | |
} | |
return holderFragmentFor(activity).getViewModelStore(); | |
} | |
// Code |
This file contains 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 сlass HolderFragment extends Fragment { | |
// Code | |
public static HolderFragment holderFragmentFor(FragmentActivity activity) { | |
return sHolderFragmentManager.holderFragmentFor(activity); | |
} | |
public static HolderFragment holderFragmentFor(Fragment fragment) { | |
return sHolderFragmentManager.holderFragmentFor(fragment); |
This file contains 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 class ViewModelProviders { | |
public static ViewModelProvider of(// arguments) { | |
// Code | |
return new ViewModelProvider(fragment.getViewModelStore(), factory); | |
} | |
public static ViewModelProvider of(// arguments) { | |
// Code | |
return new ViewModelProvider(activity.getViewModelStore(), factory); |
This file contains 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 ViewModelStore getViewModelStore() { | |
// Code | |
if (mViewModelStore == null) { | |
NonConfigurationInstances nc = | |
(NonConfigurationInstances) getLastNonConfigurationInstance(); | |
if (nc != null) { | |
mViewModelStore = nc.viewModelStore; | |
} | |
if (mViewModelStore == null) { |
This file contains 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 class Activity extends ContextThemeWrapper | |
implements LayoutInflater.Factory2, | |
//Code { | |
// Code | |
public Object getLastNonConfigurationInstance() { | |
return mLastNonConfigurationInstances != null | |
? mLastNonConfigurationInstances.activity : null; | |
} |
OlderNewer