1

Right now Im calling a new activity with an xml-view when I pause my game, but Since I do this I need to use context in my real-time code, and this is causing a memory leak. Is there any preffered way to pause the game? By pause I mean if game is over, if I die, or if I press pause-button. Would a custom dialog work just aswell? this would mean I wont have to leave my main-activity while im in-game.

Tetrad
  • 30,124
  • 12
  • 94
  • 143
Green_qaue
  • 1,893
  • 4
  • 24
  • 55

1 Answers1

1

You can use startActivityForResult, that works fine and it does not need passing the context. But in my games I've created a game object which looks like and behaves like a dialog and simply activate it on pause or game over, thus never create other activity or Android dialog.

EDIT An example using Options, which some say is safer in order to avoid out of memory errors:

private Bitmap loadBitmap(int resourceID){
            Options options = new BitmapFactory.Options();
            options.inScaled = false; 
            options.inPreferredConfig = Bitmap.Config.RGB_565; 
            Bitmap tempBmp = null;
            try{
                tempBmp = BitmapFactory.decodeStream(new BufferedInputStream(getResources().openRawResource(resourceID)), null, options);
            }catch(OutOfMemoryError e){

            }catch (Error e){

            }
            return tempBmp;
}
Lumis
  • 964
  • 1
  • 9
  • 21
  • ok, I think Ill go with a dialog, Same thing as having a game object like you have, the main thing is to not leave my activity, since that causes a leak – Green_qaue Oct 01 '12 at 21:06
  • It may not be a leak but when an activity is on pause Android may delete the big bitmaps and other resorces used in there. Check my question: http://gamedev.stackexchange.com/questions/35434/how-to-handle-loading-and-keeping-many-bitmaps-in-an-android-2d-game – Lumis Oct 01 '12 at 21:34
  • getHolder().setFormat(0x00000004); //RGB_565 load bitmpas with options.inPreferredConfig = Bitmap.Config.RGB_565; this is pretty interesting. Heard some stuff about this but didt know how to implement it. where do I put these 2 lines? can you update answer and tell? – Green_qaue Oct 01 '12 at 22:40
  • I load bitmaps like this bitmap = BitmapFactory.decodeResources(R.drawable.image); so where do I put the options.inPreferredConfig = Bitmap.Config.RGB_565; if I load them like this? – Green_qaue Oct 01 '12 at 22:41
  • See the example I posted. Some say that to use decodeStream is safer... resourceId = R.drawable.something – Lumis Oct 01 '12 at 23:24
  • nice, thanks! So to use this to load for example an image called "ball" Id just write bitmap = loadBitmap(R.drawable.ball); ? and one last qustion, why do u use inScaled = false? Hopefully this can reduce my heap-size a bit aswell :) – Green_qaue Oct 01 '12 at 23:37
  • Yes, that is the way. inScaled = false is to prevent Android resizing the bitmap according to its screen density etc. You can take the control of the bitmap size by using Bitmap.createScaledBitmap() so there is no surprises... – Lumis Oct 01 '12 at 23:51
  • I want it to resize tho, since Im downscaling all my images from xhdpi. cant have xhdpi-size on a mhdpi-phone ^^ Well ty for ur help! hopefully this will help me heap-size since Im having some issues with it, wont be able to try until 2morrow! :) – Green_qaue Oct 02 '12 at 00:22
  • Hi again! hopefully you can answer this! I tried ur method and it works great, almost cut my heap in half :) But there is a problem with using decodeStream, when I load my images with it some get smaller then they were before(on screen), but if I use decodeRecourse instead it works fine. do u know why this is? – Green_qaue Oct 29 '12 at 14:57
  • It also cant load all my images, my enemy-image wont get loaded for example, nor will my joystick-bg with opacity, or my red bullets, do you know why? does it have to do with the colors? – Green_qaue Oct 29 '12 at 15:35