14

I recently changed my app to target API Level 28 and also started using androidx instead of support libraries. After the change, I'm noticing a crash that has the following traceback

Fatal Exception: java.lang.IllegalStateException: Failure saving state: active MyFragment{22caf6fc (04a7bbf5-8806-4a45-a25d-616ed244bf18) id=0x7f1000ff} was removed from the FragmentManager
       at androidx.fragment.app.FragmentManagerImpl.saveAllState(FragmentManagerImpl.java:2301)
       at androidx.fragment.app.FragmentController.saveAllState(FragmentController.java:150)
       at androidx.fragment.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:496)
       at androidx.appcompat.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:510)
       at com.company.utils.MyAppCompatActivity.onSaveInstanceState(MyAppCompatActivity.java:161)
       at android.app.Activity.performSaveInstanceState(Activity.java:1311)
       at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1288)
       at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4166)
       at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3577)
       at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3633)
       at android.app.ActivityThread.access$1300(ActivityThread.java:164)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1491)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:160)
       at android.app.ActivityThread.main(ActivityThread.java:5541)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

All my activities are extending from a base activity MyAppCompatActivity which extends from AppCompatActivity. And in the onSaveInstanceState method of MyAppCompatActivity, I have a few log lines there. I have three fragments in this activity implemented under a ViewPager with FragmentStatePageAdapter and the activity itself is not overriding onSaveInstanceState method.

I went through a few SO posts close to this such as this one and this one, but it looks as if it's happening when we're using FragmentManager's methods to manage the fragments ourselves rather than using a ViewPagerAdapter handle it. I'm not doing that and the closest thing that I'm doing away from the usual flow is overriding FragmentStatePageAdapter's methods instantiateItem and destroyItem to help me have a method to get a fragment's reference in the code as given in this SO answer.

2
  • Try adding the fragments using the commitAllowingStateLoss() method instead on the normal commit(). Commented Nov 13, 2018 at 15:19
  • I'm not using the commit() method in my code since it is handled by the FragmentStatePageAdapter itself, which, I link to my ViewPager Commented Nov 13, 2018 at 15:23

5 Answers 5

15

Which Fragment library version did you use? There is an issue with 1.1.0-alpha01.

Sign up to request clarification or add additional context in comments.

9 Comments

Yes, we're using androidx.core:core:1.1.0-alpha01. Is there any workaround? I couldn't figure out later releases for this!
Thanks! As specified in the issuetracker, I've set the fragment dependency version to 1.0.0 and the error seems to be resolved. I've accepted the answer.
Note that the issue tracker was updated to state that alpha02 fixes this issue. Upgrading to androidx.fragment:fragment:1.1.0-alpha02 should fix this.
^ I take this back. I tried alpha02 and it's still crashing for me.
Crash not occurring after updating to 'androidx.fragment:fragment:1.1.0-alpha03'
|
5

I am using the latest gradle as below

implementation 'androidx.core:core:1.1.0-alpha03'

I have compared crashing fragment with other fragment code and found the difference as below: Crashing fragment has method setRetainInstance(true); inside onCreateView() as shown in below snippet.

     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       setRetainInstance(true);
}

I come across the link which says not to use setRetainInstance : setRetainInstance - This can only be used with fragments not in the back stack

I removed the setRetainInstance(true) for crashing fragmnet; It's working now.

How to replicate this issue: MainActivity --> HomeFragment --> FirstFragment(crashing fragment) --> HomeFragment

MainActivity loads with HomeFragment which replaced by FirstFragment and then going back to HomeFragment. Then moving my app to background and crash happens with below exception.

 java.lang.IllegalStateException: Failure saving state: active 
FirstFragment{fd50037 (4a8b618e-a0a8-45d0-aa37-ba08393b8f68)
 id=0x7f08009c} was removed from the FragmentManager

1 Comment

I have the same issue after using Android Studio to move to ActiveX
1

Add a dependency in build.gradle app

implementation 'androidx.preference:preference:1.1.0-alpha05'

it will works for me, thanks to

Refer this - https://issuetracker.google.com/issues/119256498

my issues are in run time I'm giving permission to camera and storage after that seconds the camera is open my app is crashed. I use this my issue is solved

Comments

0

I didn't have any setRetainInstance(true); When i was going through my code i saw there was another transaction which i was doing using fragment manager in onResume for the same fragment .

Comments

0

This issue is sometime caused by a double click on a button which finishes the current activity.

add :

button.setEnable="false";

for all buttons in your view ;)

enjoy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.