android - AlertDialog inside Fragment is not closed when button is pressed -
android - AlertDialog inside Fragment is not closed when button is pressed -
i have fragment within i'm using alertdialog. there no problem in closing of alertdialog in activity when used same code in fragment, alertdialog doesns't closes until press button minimum 3 times.
also after press ok button, alertdialog freezes 2 sec toast msg displayed. dialog doesn't closes.
i tried using dialog.dismiss()
. problem remains same.
here checkfragment class.
public class checckfragment extends fragment implements onitemselectedlistener { private static alertdialog alertdialog; private context context @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); context = this.getactivity(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { //other codes.... view btncheck = (button) rootview.findviewbyid(r.id.btnsavenew); btncheck.setonclicklistener(listener); } private onclicklistener listener = new onclicklistener() { @override public void onclick(view v) { createcheckbox(); alertdialog.show(); } } //function create checkbox. private void createcheckbox(){ final charsequence[] checksequences = {" low "," mid "," high "," high"}; final arraylist<integer> itemlist = new arraylist<>(); alertdialog.builder checkbuilder= new alertdialog.builder(context); checkbuilder.settitle("select school leaving reason"); checkbuilder.setmultichoiceitems(checksequences, null, new dialoginterface.onmultichoiceclicklistener() { @override public void onclick(dialoginterface dialog, int indexselected, boolean ischecked) { if (ischecked) { itemlist.add(indexselected); } else if (itemlist.contains(indexselected)) { itemlist.remove(integer.valueof(indexselected)); } } }); checkbuilder.setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { stringbuilder sb = new stringbuilder(); for(int str : itemlist){ sb.append(","); //separating contents using semi colon sb.append(str); } sb.deletecharat(0); //delete chatacter @ 0. string string = sb.tostring(); toast.maketext(context, "selected items:"+ sb, toast.length_long).show(); alertdialog.dismiss();//called dismiss here dialog doesnt closes } }); checkbuilder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { alertdialog.dismiss();//called dismiss here dialog doesnt closes } }); alertdialog = checkbuilder.create(); } }
i'm stucked dudes. please help!!!!
if working fragments dialogfragment recomended method use. if want stick alert dialog, shouldnot declare createcheckbox() in fragment class getactivity may freeze fragment , hence alertdialog not dismissed. solve issue declare createcheckbox() method in fragmentactivity class below:
private void createcheckbox(){ final charsequence[] checksequences = {" low "," mid "," high "," high"}; final arraylist<integer> itemlist = new arraylist<>(); alertdialog.builder checkbuilder= new alertdialog.builder(context); checkbuilder.settitle("select school leaving reason"); checkbuilder.setmultichoiceitems(checksequences, null, new dialoginterface.onmultichoiceclicklistener() { @override public void onclick(dialoginterface dialog, int indexselected, boolean ischecked) { if (ischecked) { itemlist.add(indexselected); } else if (itemlist.contains(indexselected)) { itemlist.remove(integer.valueof(indexselected)); } } }); checkbuilder.setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { stringbuilder sb = new stringbuilder(); for(int str : itemlist){ sb.append(","); //separating contents using semi colon sb.append(str); } sb.deletecharat(0); //delete chatacter @ 0. string string = sb.tostring(); toast.maketext(context, "selected items:"+ sb, toast.length_long).show(); alertdialog.dismiss();//called dismiss here dialog doesnt closes } }); checkbuilder.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { alertdialog.dismiss();//called dismiss here dialog doesnt closes } }); alertdialog = checkbuilder.create(); alertdialog.show(); } //*************************method selecting dialog*******************************// protected dialog oncreatedialog(int id) { switch (id) { case alert_dialog: createcheckbox(0); homecoming alertdialog; homecoming null; } } end activity class phone call createcheckbox() method this: begin(delete begin end) public class checckfragment extends fragment implements onitemselectedlistener { private static alertdialog alertdialog; private final int alert_dialog = 0; private context context @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); context = this.getactivity(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { //other codes.... view btncheck = (button) rootview.findviewbyid(r.id.btnsavenew); btncheck.setonclicklistener(listener); } private onclicklistener listener = new onclicklistener() { @override public void onclick(view v) { getactivity().showdialog(alert_dialog); //calls createmethod activity class. } } } end
this might helpful you.
android android-fragments android-alertdialog
Comments
Post a Comment