android-viewmodel

Android SavedStateHandle doesn't save in ViewModel

杀马特。学长 韩版系。学妹 提交于 2020-06-17 02:00:35
问题 I have a ViewModel which takes a SavedStateHandle parameter. I am saving a String in there like this: private fun saveString(str: String) { state.set(KEY_STRING, str) } Then I force-close my app and relaunch it, and want to retrieve the saved string like this: fun getSavedString(): String? { return state.get<String>(KEY_String) } However, it always returns null. Any ideas how to use SavedStateHandle correctly? 来源: https://stackoverflow.com/questions/62139420/android-savedstatehandle-doesnt

Koin sharedViewModel with SavedStateHandle

余生颓废 提交于 2020-06-16 01:30:32
问题 I have single activity application and number of fragments. Some of these fragments are using my viewmodel, typically like this: private val myViewModel: MyViewModel by sharedViewModel() What if I want to have the model both shared and keep its state with SavedStateHandle? I cannot figure out if this is supported and if so, how it needs to be used (declaring viewmodel as stateViewModel in hosting activity is not working). 回答1: Update: as koin 2.1.6 is around, they introduced org.koin.androidx

How to read data and listen to changes from Room database in a Service?

时光怂恿深爱的人放手 提交于 2020-06-12 03:00:13
问题 i am storing user information in a local room database. In activities and fragments I use AndroidViewModel and LiveData to listen to changes made to the database and update the UI. Now I want to analyze all of the past user data to give recommendations for future decisions. My recommendations change on every change to the database made by the user so I need to update my reommendations frequently while doing the same calculations over and over again. I was thinking about starting a service on

How to read data and listen to changes from Room database in a Service?

徘徊边缘 提交于 2020-06-12 03:00:06
问题 i am storing user information in a local room database. In activities and fragments I use AndroidViewModel and LiveData to listen to changes made to the database and update the UI. Now I want to analyze all of the past user data to give recommendations for future decisions. My recommendations change on every change to the database made by the user so I need to update my reommendations frequently while doing the same calculations over and over again. I was thinking about starting a service on

What ViewModelStoreOwner to use for ViewModelProvider in Fragment?

做~自己de王妃 提交于 2020-05-15 07:56:45
问题 I've creating a test activity that updates some text in my MyViewModel. I'd like to observe these changes in a Fragment , but when I use MyViewModel myViewModel = new ViewModelProvider(this).get(MyViewModel.class); it gives me a different instance of MyViewModel than that used in the activity, which results in my onChanged() callback in the fragment not being called. Only when I modify that same fragment code to HomeViewModel homeViewModel = new ViewModelProvider(getActivity()).get

How can I manage fragment and activity considering its creation orders?

左心房为你撑大大i 提交于 2020-05-14 13:00:10
问题 What I would like to do I would like to call Activity from Fragment to realize both transitions among fragments and activities simultaneously in an Android app. I have succeeded to call one specific method on activity file from fragment file, but I have trouble with calling an entire activity from a fragment. SampleFragment - MainActivity's fragmentMethod() is called once the button is clicked on the page Sample1Fragment - FormActivity is called once the button is clicked on the page

How to dynamically set the bundle from AbstractSavedStateViewModelFactory

丶灬走出姿态 提交于 2020-05-13 13:38:02
问题 My ViewModelFactory : class ViewModelFactory @Inject constructor( private val viewModelMap: MutableMap<Class<out ViewModel>, ViewModelAssistedFactory<out ViewModel>>, owner: SavedStateRegistryOwner, defaultArgs: Bundle? ) : AbstractSavedStateViewModelFactory(owner, defaultArgs) { @Throws(IllegalStateException::class) @Suppress("UNCHECKED_CAST") override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T { return viewModelMap[modelClass]?.create(handle)

How to dynamically set the bundle from AbstractSavedStateViewModelFactory

余生长醉 提交于 2020-05-13 13:36:05
问题 My ViewModelFactory : class ViewModelFactory @Inject constructor( private val viewModelMap: MutableMap<Class<out ViewModel>, ViewModelAssistedFactory<out ViewModel>>, owner: SavedStateRegistryOwner, defaultArgs: Bundle? ) : AbstractSavedStateViewModelFactory(owner, defaultArgs) { @Throws(IllegalStateException::class) @Suppress("UNCHECKED_CAST") override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T { return viewModelMap[modelClass]?.create(handle)

Cannot create instance of class ViewModel while using MVVM

流过昼夜 提交于 2020-04-18 06:12:09
问题 I have created ViewModel class, and trying to create an instance of my ViewModel class using ViewModelProvider. At runtime, I'm getting this error "Cannot create an instance of class className ViewModel. Here are the required codes: MainActivity.java public class MainActivity extends AppCompatActivity { MainActivityViewModel mainActivityViewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

can I make a livedata observer in my viewmodel? or should I always observer in fragment/activity?

混江龙づ霸主 提交于 2020-04-18 05:31:43
问题 I am new to MVVM. so I have 2 requests to the server from my fragment/activity, the result from the first request will be used as an input parameter for the second request. so first in my fragment, when a button is clicked then I make a request to check whether the user is banned or not, if not then this user can create a post. so first I check if a user is banned or not using this code in my fragment class CreateEventFragment : Fragment() { lateinit var model: CreateEventViewModel override