java - Handling FragmentTransaction - addToBackStack() -
i working on app has activity , fragment structure shown below:
i couldn't implement fragmenttransaction
in such way when press button in phone can go previous fragments. so, series begins in subfragment1
, ends in secondactivity:
subfragment1
->subfragment2
->subfragment3
->secondactivity
handling framgnettransaction
stuff inside fragmentb
:
//this part inside oncreateview of fragmentb fragmenttransaction fragmenttransaction = getchildfragmentmanager().begintransaction(); fragmenttransaction.add(r.id.container, subfragment1(), fragment_sub1); fragmenttransaction.addtobackstack("first"); fragmenttransaction.commit(); } public void gotosubfragment2(){ fragmenttransaction fragmenttransaction = getchildfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.container, subfragment2(), fragment_sub2); fragmenttransaction.addtobackstack("second"); fragmenttransaction.commit(); } public void gotosubfragment3(){ fragmenttransaction fragmenttransaction = getchildfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.container, subfragment3(), fragment_sub3); fragmenttransaction.addtobackstack("three"); fragmenttransaction.commit(); }
when want go next subfragment
inside subfragment
, use:
((fragmentb) this.getparentfragment()).gotosubfragment2();
and finally, in subfragment3
, use code below go secondactivity
:
intent intent = new intent(getcontext(), secondactivity.class); getcontext().startactivity(intent);
so, happens when press button... if in 1 of subfragments
, closes app. if i'm in secondactivity
, press button, app crashes , in logcat
says
unable resume activity {com.app_name.mainactivity1}: java.lang.illegalstateexception: executed.
in conclusion, question is: doing wrong? considered wrong handle fragmenttransaction
stuff inside fragment? if so, correct way? correct way add actions backstack. why button not working properly?
Comments
Post a Comment