architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

java.lang.IllegalStateException: Can't access ViewModels from detached fragment

Open ram992 opened this issue 7 years ago • 1 comments

Hello, This is a Question: I am getting the said Exception when I upgraded all my Support Libraries to the latest one. 28.0.0 I am not using any Android Architectural Components in my app, but getting the this error.

It happens when I dynamically change Fragments in an Activity, using the FragmentTransactionManager

for Example this below code added in a FragmentA is causing the Issue Bundle args = new Bundle(); args.putString("email",email); Fragment fragment = FragmentB.newInstance(args); FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.container, fragment,"FragmentB"); fragmentTransaction.addToBackStack(null); fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); fragmentTransaction.commitAllowingStateLoss(); this is working normally before I update to the latest Support Libraries Can someone tell me what am I doing wrong/ am I supposed to change any code. And a pic of Crash Logs:

Caused by java.lang.IllegalStateException: Can't access ViewModels from detached fragment at android.support.v4.app.Fragment.getViewModelStore(Fragment.java:328) at android.support.v4.app.LoaderManager.getInstance(LoaderManager.java:128) at android.support.v4.app.Fragment.performDestroyView(Fragment.java:2687) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1533) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852) at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3269) at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:3260) at android.support.v4.app.FragmentController.dispatchDestroy(FragmentController.java:274) at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:419) at android.support.v7.app.AppCompatActivity.onDestroy(AppCompatActivity.java:210)

ram992 avatar Jan 03 '19 04:01 ram992

Please file issues with Fragments on the official issue tracker with a sample project that reproduces your issue.

ianhanniballake avatar Mar 09 '19 00:03 ianhanniballake