android - IllegalStateException when adding & replacing Fragments; commit vs commitAllowingStateLoss -
it rare happens, app crash due illegalstateexception when adding , replacing fragments. here how doing it, animation.
private void addfragmentreplace(int containerid, fragment fragment) { // check if fragment has been added fragment temp = mfragmentmanager.findfragmentbytag(fragment.gettag()); if (!utils.checkifnull(temp) && temp.isadded()) { return; } // replace fragment , transition animation mfragmentmanager.begintransaction().setcustomanimations(r.anim.ui_slide_in_from_bottom_frag, r.anim.ui_slide_out_to_bottom_frag).replace(containerid, fragment).addtobackstack(null) .commit(); } i have researched changing "commit()" "commitallowingstateloss()" solution? prevent crashes, however, won't cause other conflicts such fragment not displaying @ times or other? following odd improvement above code snippet?
private void addfragmentreplace(int containerid, fragment fragment) { // check if fragment has been added fragment temp = mfragmentmanager.findfragmentbytag(fragment.gettag()); if (!utils.checkifnull(temp) && temp.isadded()) { return; } // replace fragment , transition animation try { mfragmentmanager.begintransaction().setcustomanimations(r.anim.ui_slide_in_from_bottom_frag, r.anim.ui_slide_out_to_bottom_frag).replace(containerid, fragment).addtobackstack(null) .commit(); } catch (illegalstateexception e) { e.printstacktrace(); mfragmentmanager.begintransaction().setcustomanimations(r.anim.ui_slide_in_from_bottom_frag, r.anim.ui_slide_out_to_bottom_frag).replace(containerid, fragment).addtobackstack(null) .commitallowingstateloss(); } } thanks in advance. concerns come documentation commitallowingstateloss() reads
like {@link #commit} allows commit executed after activity's state saved. dangerous because commit can lost if activity needs later restored state, should used cases okay ui state change unexpectedly on user.
some tips or advice on appreciated. thanks!
Comments
Post a Comment