0

I have a little app and want to save some data when exiting the application. I intended to do this in a method, which I call when the application is exited. I guess I could write in the necessary code in the onDestroy() of every possible activity. But is there a more efficient way that I only have to write the code once and it counts for every activity or for the entire application?

Zonico
  • 192
  • 1
  • 11

1 Answers1

0

It is possible to get a callback when your application goes to background. Implement the onTrimMemory method in your Application class and check for TRIM_MEMORY_UI_HIDDEN level.

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);

        if (level == TRIM_MEMORY_UI_HIDDEN) {
            // your app went to background now. 
        }
    }
Bob
  • 13,447
  • 7
  • 35
  • 45