AndroidX , Can't access ViewModels from detached fragment
See original GitHub issueDescription
java.lang.IllegalStateException: Can't access ViewModels from detached fragment
at androidx.fragment.app.Fragment.getViewModelStore(Fragment.java:328)
at androidx.lifecycle.ViewModelProvider.<init>(ViewModelProvider.java:67)
at leakcanary.internal.ViewModelClearedWatcher$Companion.install(ViewModelClearedWatcher.kt:56)
at leakcanary.internal.AndroidXFragmentDestroyWatcher$fragmentLifecycleCallbacks$1.onFragmentCreated(AndroidXFragmentDestroyWatcher.kt:38)
at androidx.fragment.app.FragmentManagerImpl.dispatchOnFragmentCreated(FragmentManager.java:3496)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)
Version Information
- LeakCanary version: 2.4
- Android OS version: all
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Android, Can't access ViewModels from detached fragment
The #1 problem I see (also mentioned by some other user(s) is that you appear to be calling a public method in a...
Read more >Android, Can't access ViewModels from detached fragment ...
The #1 problem I see (also mentioned by some other user(s) is that you appear to be calling a public method in a...
Read more >Can't access ViewModels from detached fragment at androidx ...
经过排查定位放线xml中默认将ViewPager给Gone了,也就是在ViewPager中初始化了fragment但是fragment还不可见,viewmodel对象当前还没有实例化。
Read more >fragment/fragment/src/main/java/androidx ... - Google Git
throw new IllegalStateException("Can't access ViewModels from detached fragment");. } return mFragmentManager.getViewModelStore(this);.
Read more >Source Code for Fragment.java - AndroidX Tech
... safe to access the view lifecycle is * {@link #onCreateView(LayoutInflater, ... IllegalStateException("Can't access ViewModels from detached fragment"); ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@pyricau
Found the reason, it is our developer’s problem. It is because he overwrites getContext() in the business fragment, and the context value has not been initialized when LeakCanary calls getContext().
thank you very much.
The version of AndroidX I use is
Wait a moment for me to reproduce the scene…