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.


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;
                tempBmp = BitmapFactory.decodeStream(new BufferedInputStream(getResources().openRawResource(resourceID)), null, options);
            }catch(OutOfMemoryError e){

            }catch (Error e){

            return tempBmp;
| improve this answer | |
  • \$\begingroup\$ 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 \$\endgroup\$ – Green_qaue Oct 1 '12 at 21:06
  • \$\begingroup\$ 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: gamedev.stackexchange.com/questions/35434/… \$\endgroup\$ – Lumis Oct 1 '12 at 21:34
  • \$\begingroup\$ 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? \$\endgroup\$ – Green_qaue Oct 1 '12 at 22:40
  • \$\begingroup\$ 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? \$\endgroup\$ – Green_qaue Oct 1 '12 at 22:41
  • \$\begingroup\$ See the example I posted. Some say that to use decodeStream is safer... resourceId = R.drawable.something \$\endgroup\$ – Lumis Oct 1 '12 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.