IllegalStateException - Fragment support library

前端 未结 3 934
情话喂你
情话喂你 2020-12-11 00:35

I have a serious problem with my App continously crashing on some users devices with the following exception in an Activity onStart method:

Caused by: java.l         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 01:05

    Read This Article.

    And,

    This one.

    Once the issue is understood, go ahead and change things properly:

    Figure out why your transactions are happening outside your onResume state. Why are these things happening when they shouldn't be. Why are you manually adding and removing fragments with regard to the lifecycle of you app rather than when the user requests. You should maybe need one time in the onCreate() or when the user invokes something. But why are you messing with them in onResume() in the first place? Don't do that.

    If you change all the .commit() to .commitAllowStateLoss() then the crashes will go away. But, you're better off not doing that. You are better off making sure you never change your fragments except when the app is fully live. Sometimes that means never loading a fragment from the OnActivityResult() but rather flagging to run in your onPostResume(), and avoiding performing UI changes in async threads that wrongly assume you couldn't have killed the activity in the meantime.

    If you just want to spackle everything, .commitAllowStateLoss() will do that. If you want to do it right, make sure you don't fiddle with your fragments after things die.

    One of the more recent forms of Android circa Oreo I think, changed this to avoid this annoying error. My answer seems a bit vague but it's because the bug is in the paradigm. You entirely can hit that with various code elements and fairly routinely.

提交回复
热议问题