android - Backstack Fragment not appearing in front on Back button prerssed -
i have gone through many stackoverflow question before writing this. confused guy backstack in fragment.
i have added 3 fragment on same container inside activity
fragment 1 :
private void addlandingfragment() { landingpagefragment = landingpagefragment.newinstance(); fragmenttransaction transaction = manager.begintransaction(); transaction.add( r.id.container, landingpagefragment, landing_page_fragment_tag ); transaction.commit(); }
fragment 2 :
public void addintrofragment() { fragment2 = introfragment.newinstance(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace( r.id.container, fragment2, intro_page_fragment_tag); transaction.addtobackstack(fragment2.getclass().getname() ); transaction.commit(); }
fragment 3 :
public void ongetstartedclicked() { fragment3= connectfragment.newinstance(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace( r.id.container, fragment3,connect_page_fragment_tag ); transaction.commit(); }
now want when user presses button on fragment 3 should come on first fragment have overrided onbackpressed() method.
@override public void onbackpressed() { manager.popbackstack(fragment2.getclass().getname() ,fragmentmanager.pop_back_stack_inclusive ); }
but nothing happening on screen keeps fragment 3 running.
update
when navigating
fragment1 > fragment2
and presses button on fragment2, coming fragment1 if move
fragment1 > fragment2> fragment3
i getting stack entry count 1 on onbackpressed()
method on device screen still shows fragment3. pressing button again exit me app fragment1 wont come on screen. puzzling why happening ?
any solution achieve this.
calling replace()
remove previous fragment, fragment 1 should called using replace()
, , fragment 2 & 3 should called using add()
, should add last transaction stack (calling fragment 3)
like this:
fragment 1:
private void addlandingfragment() { landingpagefragment = landingpagefragment.newinstance(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace( r.id.container, landingpagefragment, landing_page_fragment_tag ); transaction.commit(); }
fragment 2:
public void addintrofragment() { fragment2 = introfragment.newinstance(); fragmenttransaction transaction = manager.begintransaction(); transaction .hide(landingpagefragment.this); transaction.add( r.id.container, fragment2, intro_page_fragment_tag); transaction.addtobackstack(fragment2.getclass().getname() ); transaction.commit();
}
fragment 3:
public void ongetstartedclicked() { fragment3= connectfragment.newinstance(); fragmenttransaction transaction = manager.begintransaction(); fragmentmanager.popbackstackimmediate(); // remove fragment 2 transaction.add( r.id.container, fragment3,connect_page_fragment_tag ); transaction.addtobackstack(fragment3.getclass().getname() ); transaction.commit(); }
finally onbackpressed
should this:
@override public void onbackpressed() { fragmentmanager.popbackstackimmediate(); fragmenttransaction.show(landingpagefragment.this); }
therefore onbackpressed
pop top fragment on stack (fragment 3), , since fragment 2 popped before adding fragment 3, onbackpressed
display first fragment.
Comments
Post a Comment