Search code examples
androidandroid-fragmentsfragmentandroid-adapterdata-loss

Fragment force close while setting adapter


exception :: Fragment no longer exists for key f0: index 0

while setting FragmentStatePagerAdapter in activity

LOG ::

 Caused by: java.lang.IllegalStateException: Fragment no longer exists for key f0: index 1
                                                                             at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:668)
                                                                             at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
                                                                             at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1319)
                                                                             at android.view.View.dispatchRestoreInstanceState(View.java:13758)
                                                                             at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2889)
                                                                             at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895)
                                                                             at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895)
                                                                             at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895)
                                                                             at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895)
                                                                             at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895)
                                                                             at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895)
                                                                             at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895)
                                                                             at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895)
                                                                             at android.view.View.restoreHierarchyState(View.java:13736)
                                                                             at android.support.v4.app.Fragment.restoreViewState(Fragment.java:468)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1094)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
                                                                             at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2042)
                                                                             at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:165)
                                                                             at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:543)
                                                                             at com.smartzip.smartcheckin.activity.BaseFragmentActivity.onStart(BaseFragmentActivity.java:22)
                                                                             at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1259)
                                                                             at android.app.Activity.performStart(Activity.java:6026)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:135) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:702) 

Solution

  • Add code in FragmentStatePagerAdapter.

    @Override
    public Parcelable saveState() {
    return null;
     }
    

    and

     @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
    //do nothing here! no call to super.restoreState(arg0, arg1);
    }