Friday, 15 July 2011

android when checkbox is checked show fragment -



android when checkbox is checked show fragment -

i fresh @ android programming , in simple program,

i trying show fragment when checkbox checked , remove fragment when checkbox not checked.

in code, when checked checkbox, fragment shown , when disable checkbox fragment removed. when seek show 1 time again can not succeed it.

this part of code checkbox

checkbox.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // todo auto-generated method stub if(checkbox.ischecked()) { checkbox.settext("it on."); myfragment fragment = (myfragment)getfragmentmanager().findfragmentbyid(r.id.container); if (fragment == null) { getfragmentmanager().begintransaction() .add(r.id.container, new myfragment()). commit(); } } else { checkbox.settext("it off."); myfragment fragment = (myfragment)getfragmentmanager().findfragmentbyid(r.id.container); if (fragment != null) { getfragmentmanager().begintransaction() .remove(fragment) .commit(); } } } });

is there can help me?

you cannot add together because removed completely, seek detatch it. detaching fragment keeps state.

try this:

getfragmentmanager().begintransaction() .detach(fragment) .commit(); }

remove means fragment instance cannot re-attached. have add together 1 time again fragment transaction.

android checkbox fragment

No comments:

Post a Comment