java - FragmentManager: popBackStack() and IllegalStateException -
i have activity:
public class myactivity extends appcompatactivity implements fragmentmanager.onbackstackchangedlistener { ... @override public void onbackpressed() { if (getfragmentmanager().getbackstackentrycount() > 0) getfragmentmanager().popbackstack(); } }
simplefragmentmanager:
private static void moveto(basefragment fragment) { if(fragment != null) { fragmentmanager fragmentmanager = _activity.getfragmentmanager(); fragmenttransaction ft = fragmentmanager.begintransaction(); //this //ft.settransition(fragmenttransaction.transit_fragment_close); //or //ft.setcustomanimations(r.animator.enter, r.animator.exit, r.animator.enter, r.animator.exit); //or //ft.setcustomanimations(r.animator.enter, r.animator.exit); basefragment currentfragment = getcurrentfragment(); if(fragment != currentfragment) { ft.replace(_containerid, fragment, fragment.gettype()); if (currentfragment != null) { ft.addtobackstack(null); } ft.commit(); } } }
example fragment:
public class afragment extends basefragment { @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { if(view == null) { view = new alayout(container.getcontext()); view.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.match_parent, relativelayout.layoutparams.match_parent)); } return view; } }
>>problem<<
the test this, i'm on fragment a, turn on fragment b, change process of animation pieces, click button @ bottom of screen.
from first time can not happen, making these steps several times, application crashes error:
e/androidruntime: fatal exception: main process: com.my.application, pid: 2103 java.lang.illegalstateexception: specified child has parent. must call removeview() on child's parent first. @ android.view.viewgroup.addviewinner(viewgroup.java:4309) @ android.view.viewgroup.addview(viewgroup.java:4145) @ android.view.viewgroup.addview(viewgroup.java:4086) @ android.view.viewgroup.addview(viewgroup.java:4059) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:985) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1148) @ android.app.backstackrecord.run(backstackrecord.java:793) @ android.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1535) @ android.app.fragmentmanagerimpl$1.run(fragmentmanager.java:482) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)
this error occurs if use transition animation, here this:
ft.settransition(fragmenttransaction.transit_fragment_close);
or
ft.setcustomanimations(r.animator.enter, r.animator.exit, r.animator.enter, r.animator.exit);
or
ft.setcustomanimations(r.animator.enter, r.animator.exit);
if not use transition animations, there no error!
i have found many of these topics, , found there bug api:
1) https://code.google.com/p/android/issues/detail?id=77670
2) https://code.google.com/p/android/issues/detail?id=89244
my sdk version:
compilesdkversion 23 buildtoolsversion '23.0.1'
the important thing, in fragment did not delete view this:
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { ... viewgroup parent = (viewgroup) view.getparent(); if(parent != null) parent.removeview(view); }
because want use single view!
tell me please possible correct error?
>>p.s. 1 of variants of solution: disable button while there animation fragments.
Comments
Post a Comment