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.
1 Answer
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;
}
-
\$\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\$ Oct 1, 2012 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\$– LumisOct 1, 2012 at 21:34
-
\$\begingroup\$
getHolder().setFormat(0x00000004); //RGB_565
load bitmpas withoptions.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\$ Oct 1, 2012 at 22:40 -
\$\begingroup\$ I load bitmaps like this
bitmap = BitmapFactory.decodeResources(R.drawable.image);
so where do I put theoptions.inPreferredConfig = Bitmap.Config.RGB_565;
if I load them like this? \$\endgroup\$ Oct 1, 2012 at 22:41 -
\$\begingroup\$ See the example I posted. Some say that to use decodeStream is safer... resourceId = R.drawable.something \$\endgroup\$– LumisOct 1, 2012 at 23:24