In the summer of last year I started refactoring my Android application with Android's architecture components (Room, ViewModel, LiveData).
I have two Room repositories, one of them is accessed by multiple views (fragments) of the application. Because of that I used an AndroidViewModel
, which has access to this repository and which is initialized in my MainActivity
.
new ViewModelProvider(this).get(CanteensViewModel.class);
In my two fragments I accessed this ViewModel by
new ViewModelProvider(getActivity()).get(CanteensViewModel.class);
Until yesterday that worked perfectly. But then I updated my dependencies and since androidx.lifecycle
version 2.2.0 this does not work anymore. I always get an exception (siehe EDIT 2):
Caused by: java.lang.InstantiationException: java.lang.Class<com.(...).CanteensViewModel> has no zero argument constructor
So I checked the docs and as I understood right I should/could now use
ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication()).create(CanteensViewModel.class);
to get my ViewModel. But with this approach I can't add the owner
(parameter of ViewModelProvider
s constructor), which results in the problem, that I can't really access the ViewModel I created in the Activity from inside my fragments.
Is there a way I can access the Activity's ViewModel from inside the fragments? Or would it be better to recreate the ViewModel in each fragment by
ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication()).create(CanteensViewModel.class);
instead of creating it inside the Activity?
EDIT:
It seems to work, when I use the other constructor of ViewModelProvider
, where a AndroidViewModelFactory
is the second parameter.
new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(CanteensViewModel.class);
Doing this in my MainActivity
I can access the CanteensViewModel
in my Fragment
via
new ViewModelProvider(requireActivity()).get(CanteensViewModel.class);
EDIT 2
Stacktrace for above mentioned exception:
2020-02-28 14:30:16.098 25279-25279/com.pasta.mensadd E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pasta.mensadd, PID: 25279
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pasta.mensadd/com.pasta.mensadd.ui.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.pasta.mensadd.ui.viewmodel.CanteensViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2795)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1602)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6543)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.pasta.mensadd.ui.viewmodel.CanteensViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.pasta.mensadd.ui.MainActivity.onCreate(MainActivity.java:70)
at android.app.Activity.performCreate(Activity.java:7023)
at android.app.Activity.performCreate(Activity.java:7014)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2748)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)?
at android.app.ActivityThread.-wrap11(Unknown Source:0)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1602)?
at android.os.Handler.dispatchMessage(Handler.java:106)?
at android.os.Looper.loop(Looper.java:164)?
at android.app.ActivityThread.main(ActivityThread.java:6543)?
at java.lang.reflect.Method.invoke(Native Method)?
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)?
Caused by: java.lang.InstantiationException: java.lang.Class<com.pasta.mensadd.ui.viewmodel.CanteensViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)?
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)?
at com.pasta.mensadd.ui.MainActivity.onCreate(MainActivity.java:70)?
at android.app.Activity.performCreate(Activity.java:7023)?
at android.app.Activity.performCreate(Activity.java:7014)?
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2748)?
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)?
at android.app.ActivityThread.-wrap11(Unknown Source:0)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1602)?
at android.os.Handler.dispatchMessage(Handler.java:106)?
at android.os.Looper.loop(Looper.java:164)?
at android.app.ActivityThread.main(ActivityThread.java:6543)?
at java.lang.reflect.Method.invoke(Native Method)?
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)?
```
See Question&Answers more detail:
os