Android using fragments add to back stack wont work -
i building calculator app using fragment , want calculator fragment show when button pressed on menu item fragment. have been using addtobackstac(null) not working me.
here code below
public class mainactivity extends actionbaractivity {
fragmentmanager fragmentmanager; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if(savedinstancestate == null){ fragmentmanager = getfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction().addtobackstack(null); calculatorfragment calc = new calculatorfragment(); fragmenttransaction.add(r.id.rellejaut, calc); fragmenttransaction.commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main, menu); return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.item) { about= new about(); // insert fragment replacing existing fragment fragmentmanager.begintransaction().addtobackstack(null).replace(r.id.rellejaut, about).commit(); return true; } return super.onoptionsitemselected(item); }
}
you need first add() or replace() fragment in fragmenttransaction , call addtobackstack() work.
call way:
fragmentmanager.begintransaction().replace(r.id.rellejaut,about).addtobackstack(null).commit();
Comments
Post a Comment