Monday, June 10, 2013

Dialog/orientation change memory leaks...

Well, today I kept battling a memory leak in my opening screen for Perspective when rotating the phone.  A quick search yielded plenty of references to orientation changes when rotating.  There were quite a few work arounds, but I found a simple one that really made it simple for me so I figured I'd share.

To fix the problem in my PopDialog class that extends a Dialog, I added a

static PopDialog pd=null;  

Then I added this just after the onCreate


and finally I added this method

 public void dismissing(){


OK, that's the hard part.  Now in my main activity, I referenced it like this

 PopDialog popdialog;
 private static boolean opened=false;

after onCreate in that activity

popdialog=new PopDialog(this);

and finally in the onPause

protected void onPause() {
if(opened){ popdialog.dismissing();}

I forgot, add this to the button you call the Dialog from because sometimes it doesn't call onCreate.


This way when rotating, it always has the proper reference to the active dialog and calls to dismiss it destroying it.  I had to update it a bit by adding the opened in there.  This is won't let it call dismiss before the dialog is created.

No comments:

Post a Comment