views:

60

answers:

1

I have a game running on android.

basically, it's structure is like lunarlander

I started my activity , using the layout to start the class running.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <my.darling.gameView
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/>

</FrameLayout>

When I pressed home, i can return to my game everytime. I always shut down the thread and create a new one.

The problem occurs when I pressed BACK botton.

I think my game finished. but after 4 times of "click the game" -> "click BACK"

There comes an error -> "stopped unexpectedly"

I've override the function : onPause() and called finish(). But it still happens.

can anyone help me?

A: 

The problem I am facing is :

I allocate some Bitmaps in my UIthread. System doesn't free the memory if you press the BACK button.

Even if the activity is destroyed, it doesn't give back memory used by Bitmap.

So When I tried to test my game by pressing back and forth between BACK and my game, the VM shut down due to the lack of external memory.

The Bitmaps I decoded from resources has filled all my memory.

The easiest way to fix it :

protected void onPause()
{
    super.onPause();
    System.gc();
}

since there is a bug of cleaning the memory of Bitmap in android. We manually call GC to force it to clean our Bitmaps. It's a bug.

or use Bitmap.recycle() if you want to free the Bitmap immediately.

http://mobi-solutions.blogspot.com/2010/08/how-to-if-you-want-to-create-and.html

http://code.google.com/p/android/issues/detail?id=8488

Steven Shih