Search code examples
androidiosuiapplicationdelegate

iOS AppDelegate equivalent in Android


I have a simple Android app with one Activity. This Activity downloads a small XML file and displays the contents to the user. Currently I kick-off the download in the Activity's onStart method.

It's my understanding that Activities are destroyed and re-created when the device orientation changes. So when users rotate my app the XML file is downloaded again. I'd like the app to download the file just once as it doesn't change more than a few times per day. Hence I'd like some object to retain the downloaded data so the local data can be re-used when the device orientation changes. What's the ideal object in Android to retain the data?

On iOS I'd use the AppDelegate to download the data once and retain it. Then the viewController that displays the data can just re-use the local data should it be destroyed and re-created.


Solution

  • I'm assuming you've seen this page:

    http://developer.android.com/guide/topics/data/data-storage.html#pref

    Depending on the kind of data from the XML file (if it's fairly simple data), using "Shared Preferences" could be a simple way to go. You could rewrite the new data from XML when you need to while still being able to use it from the shared preferences.