{ super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } override fun onRetainCustomNonConfigurationInstance(): Any { return Dog("chip", 6) } } data class Dog(val name: String, val age: Int) อ͍࣋ͨ͠ΠϯελϯεΛฦ٫
{ super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val dog = lastCustomNonConfigurationInstance as Dog? ?: Dog("wasa", 18) } override fun onRetainCustomNonConfigurationInstance(): Any { return Dog("chip", 6) } } data class Dog(val name: String, val age: Int) औಘ
@Suppress("UNCHECKED_CAST") override fun <T : ViewModel?> create(modelClass: Class<T>): T { return UserViewModel(repo) as T } } ViewModelProvider.FactoryΛܧঝͯ͠ҾΛ༩͑ͨΓͰ͖Δ DaggerͰɺFactoryʹ@Singleton͚ͭͯཧͨ͠Γ ViewModelInjectͨ͠Γ͢Δ߹ɺ͜ͷܧঝ͕ඞཁʹͳΓ·͢ https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/viewmodel/GithubViewModelFactory.java
the give class. */ public static class NewInstanceFactory implements Factory {} /** * {@link Factory} which may create {@link AndroidViewModel} and * {@link ViewModel}, which have an empty constructor. */ public static class AndroidViewModelFactory extends ViewModelProvider.NewInstanceFactory {} ͦͷଞ
User? = null fun getUser(): User? { if (user == null) { load() } return user } private fun load() { user = User("Hanyu", 23) } } data class User(val name: String, val age: Int) http://bit.ly/AAC-VM
user: MutableLiveData<User>? = null fun getUser(): MutableLiveData<User>? { if (user == null) { user = MutableLiveData() load() } return user } private fun load() { user?.postValue(User("Hanyu", 23)) } } data class User(val name: String, val age: Int) http://bit.ly/AAC-VM
Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val user = ViewModelProviders.of(this).get(UserViewModel::class.java) } } class BottomFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val user = ViewModelProviders.of(this).get(UserViewModel::class.java) } } http://bit.ly/AAC-VM