AndroidX , Can't access ViewModels from detached fragment

See original GitHub issue

Description

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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
LiamUcommented, Jan 30, 2021

@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.

0reactions
LiamUcommented, Jan 30, 2021

The version of AndroidX I use is

force group:'androidx.fragment', name:'fragment', version: '1.0.0'
force group:'androidx.activity', name:'activity', version: '1.0.0'

Wait a moment for me to reproduce the scene…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found