android - Out of memory, showing AlertDialog and making changes in orientation -
i have activity containing 2 imagebuttons. if user clicks alertdialog shown. while alert shown, if orientation changed , forth few times application crashes message:
error/dalvikvm-heap(10988): 3363556-byte external allocation large process. error/dalvikvm(10988): out of memory: heap size=4935kb, allocated=2594kb, bitmap size=19579kb error/graphicsjni(10988): vm won't let allocate 3363556 bytes
if alertdialog not there, application doesn't crash.
public class startpageview extends activity implements onclicklistener, android.content.dialoginterface.onclicklistener { private static final string tag = "startpageview"; private imagebutton vacbutton; private imagebutton sickbutton; private alertdialog alert; private static boolean alertdismissedbylifecycle = false; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.startpage); vacbutton = (imagebutton)findviewbyid(r.id.gotovac_btn); vacbutton.setonclicklistener(this); sickbutton = (imagebutton)findviewbyid(r.id.gotosick_btn); sickbutton.setonclicklistener(this); if(alertdismissedbylifecycle) { alertdismissedbylifecycle = false; showalert(); } } @override protected void onstop() { if(alert != null && alert.isshowing()) { alertdismissedbylifecycle = true; alert.dismiss(); } super.onstop(); } //go vacation view or sick view //user clicked on vacview button or sickview button @override public void onclick(view v) { if(v.equals(this.vacbutton)) { intent = new intent(this, vacationleaveview.class); startactivity(i); } if(v.equals(this.sickbutton)) { intent = new intent(this, sickleaveview.class); startactivity(i); } } //log out //user pressed button @override public void onbackpressed() { showalert(); } //onclicklistener logging out warning @override public void onclick(dialoginterface dialog, int which) { if(which == dialoginterface.button_positive) { maincontroller.getinstance().clearallchildren(); dialog.cancel(); this.finish(); } else dialog.cancel(); } //dialog private void showalert() { if(alert == null) { alertdialog.builder alertdialog = new alertdialog.builder(this); alertdialog.setmessage("vill du logga ut från förskoleportalen?"); alertdialog.setpositivebutton("ja", this); alertdialog.setnegativebutton("nej", this); alert = alertdialog.create(); // title alertdialog alert.settitle("logga ut"); } alert.show(); }
your context of "this" cause. need explicitly specify context avoid issue. issue discussed here: avoiding memory leaks
the problem ... when add images inside activity context of "this" , change orientation image gets passed along else "this" oncreate() method part of bundle. on , on again , end 1 image per orientation change in memory. android documentation speaks verbatim.
Comments
Post a Comment