Question

I am developing an application and a problem occured during testing. The main activity shows database records. When system memory is low (I push the home button and start a lot of apps) the garbage collector deletes the database records, and if I go back to the app, nothing is displayed on the activity (no records), and the app force closes on any actions (back, home, touch, etc.)

Any ideas to solve the problem?

Was it helpful?

Solution 3

Problem solved. It came out when system memory was extremely low (I launched 10 other applications to test my application), and I solved it by using the singleton pattern on the database class.

OTHER TIPS

In Manifest File

<application name=".MainAppActivity" >
   .
   .
   .
</application>

In MainActivity

public class MainAppActivity extends Application {
   .
   .
   .
   .
   @Override
   public void onLowMemory(){
      System.gc();
   }
}

You could make a check in onResume, if your records array for example is empty or null, reload them (maybe reload them in a service) or you could even save them in a file when you first load them and save the file in onPause() and later in onResume() you can load this file.

Anyway, if you think your app takes too much memory, you could try to use System.gc() to try to clean some garbage, but this option may not be usefull if user starts other apps and fill memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top