Created
January 19, 2019 10:44
-
-
Save andreikastsiuk/09fd397ebf17cee6cbae7e449cd94572 to your computer and use it in GitHub Desktop.
A provider factory that persists ViewModels. Used if the view model has a parameterized constructor.
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
import android.arch.lifecycle.ViewModel; | |
import android.arch.lifecycle.ViewModelProvider; | |
/** | |
* A provider factory that persists ViewModels {@link ViewModel}. | |
* Used if the view model has a parameterized constructor. | |
*/ | |
public class ViewModelProviderFactory<V> implements ViewModelProvider.Factory { | |
private V viewModel; | |
public ViewModelProviderFactory(V viewModel) { | |
this.viewModel = viewModel; | |
} | |
@Override | |
public <T extends ViewModel> T create(Class<T> modelClass) { | |
if (modelClass.isAssignableFrom(viewModel.getClass())) { | |
return (T) viewModel; | |
} | |
throw new IllegalArgumentException("Unknown class name"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment