Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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;
}
share|improve this answer
    
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 –  Max Oct 1 '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: gamedev.stackexchange.com/questions/35434/… –  Lumis Oct 1 '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? –  Max Oct 1 '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? –  Max Oct 1 '12 at 22:41
    
See the example I posted. Some say that to use decodeStream is safer... resourceId = R.drawable.something –  Lumis Oct 1 '12 at 23:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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