0

My app has a class that extends Application and another class which is an API to get data from the Internet using a singleton pattern.
When the Application class loads, onCreate() will instantiate the singleton to be used throughout the app.

At a certain point, the user my request a transportation route, which will cause Maps to load and send my app to background.
If the user return to the app soon enough (ie, the app hasn't been fully terminated yet), all the static variables from the singleton are now null.

What is the proper way to save those variables before going to background and retrieve them back after returning to foreground?

Guilherme
  • 7,839
  • 9
  • 56
  • 99

1 Answers1

1

Take a look on SharedPreferences. Use it in onPause method of activity.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • I know how to use them, but I need to detect when the app will go background and when it returns. – Guilherme Apr 15 '14 at 20:54
  • onPause -> onResume method of activity – Divers Apr 15 '14 at 20:56
  • But that will run when the Activity pauses, not the Application, right? So if I start another activity, that code is subject to be run even though it is not needed. – Guilherme Apr 15 '14 at 20:58
  • 1
    Right, but there is no other callback to do it, onPause is the best and possible the only place. – Divers Apr 16 '14 at 05:46
  • I used `SharedPreferences` in combination with this answer: http://stackoverflow.com/a/6357860/1262783 – Guilherme Apr 16 '14 at 14:37