Monday, August 28, 2017

java.lang.IllegalStateException: Fragment no longer exists for key f0: index 1

Leave a Comment

I am having couple of fragment in an Activity. After doing some process I am closing the fragment using the below code.

if (getActivity().getSupportFragmentManager().getBackStackEntryCount() > 0) {                         getActivity().getSupportFragmentManager().popBackStackImmediate(); } 

But on line popBackStackImmediate() its throwing the error

    Process: com.TestProject.testpro, PID: 17966  java.lang.IllegalStateException: Fragment no longer exists for key f0: index 1      at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:879)      at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:215)      at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1481)      at android.view.View.dispatchRestoreInstanceState(View.java:14746)      at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3121)      at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3127)      at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3127)      at android.view.View.restoreHierarchyState(View.java:14724)      at android.support.v4.app.Fragment.restoreViewState(Fragment.java:475)      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1329)      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)      at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:807)      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2360)      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)      at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)      at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:823)      at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:776)      at com.TestProject.testpro.application.fragments.WFMUpgradeToAffinityFragmentAN$WFMStoreProfileIdReceiver.onReceive(WFMUpgradeToAffinityFragmentAN.java:1064)      at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)      at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)      at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)      at android.os.Handler.dispatchMessage(Handler.java:102)      at android.os.Looper.loop(Looper.java:148)      at android.app.ActivityThread.main(ActivityThread.java:5417)      at java.lang.reflect.Method.invoke(Native Method)      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

There is no other crash which close the fragment. I cant find the exact solution for this issue. Please help me on this.

Edited:

I am using the below function to call the fragment.

void showContentFragment(Fragment newFragment, String backStackName) {     //Log.e(TAG, "showContentFragment- " + "BackstackName: "+ backStackName);     FragmentManager mFragmentManager = getActivity().getSupportFragmentManager();     if (newFragment != null) {         //updateNavigation(backStackName);         FragmentTransaction ft = mFragmentManager.beginTransaction();         ft.replace(R.id.fragment_container, newFragment);         if (backStackName != null && !backStackName.isEmpty()) {             ft.addToBackStack(backStackName);         }          ft.commit();     } } 

5 Answers

Answers 1

Please follow this steps to add fragments from your activity

 getSupportFragmentManager().beginTransaction()                 .replace(R.id.frame, yourFragment, yourFragment.class.getSimpleName()).addToBackStack(yourFragment.class.getSimpleName())                 .commit(); 

using this code for calling popBackStack() from your fragment

    if (getFragmentManager().getBackStackEntryCount() > 0) {                         getFragmentManager().popBackStack(); } 

Make sure that you are importing right fragment class

import android.support.v4.app.Fragment; 

Answers 2

add Below code in your Fragment classes , according to doc it returns the Transition to use to move Views out of the Scene when the Fragment is preparing to close.

@Override     public Object getReturnTransition() {         return super.getReturnTransition();     } 

Answers 3

Your stacktrace shows you are using both a ViewPager and manipulating the backstack yourself.

This is probably not a good idea - either let your ViewPager manage the backstack (which it does well) or manage it entirely yourself.

Note that in managing the backstack yourself you will have to account for restoring instance state (which features in your stacktrace) and the Fragment lifecycle.

This means in conditions of low memory your Activity and Fragments will be stopped. If you have not saved the instance state correctly, when they are restarted you will get errors like the above one. If the workflow of your Fragments is simple, you can avoid all of this by using a ViewPager.

Answers 4

public void showContentFragment(Fragment fragment, Bundle bundle, boolean addToBackStack) {         if (bundle != null)             fragment.setArguments(bundle);          tag = fragment.getClass().getSimpleName();         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();         ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);         ft.replace(R.id.container_internal, fragment, tag);         fragment.setRetainInstance(true); // IMPORTANT          if (addToBackStack)             ft.addToBackStack(tag);          try {             ft.commit();          } catch (Exception ex) {             ex.printStackTrace();             ft.commitAllowingStateLoss();         } 

Answers 5

First, you don't need to call getActivity().getSupportFragmentManager(). getFragmentManager() is enough to call FragmentManager.

If you are in Support Fragment getFragmentManager() will give you supportFragmentManager() or it will give you app.Fragment Manager.

If you are calling the PopBackstack inside the ViewPager, it won't work. If you are using viewPager inside another fragment. you should call popBackstack only in the parent fragment. Not in the viewPager.

Try the below code it will clear the stack according to the back stack name.

getFragmentManager.popBackStackImmediate(YourBackStackName, FragmentManager.POP_BACK_STACK_INCLUSIVE) 

Docs: https://developer.android.com/reference/android/app/FragmentManager.html#popBackStackImmediate(java.lang.String, int)

Hope it helps :)

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment