tags:

views:

84

answers:

2

I have an application that is driven by a configuration XML: various app properties are loaded at the app start-time by parsing the XML and initializing static variables of some class. The data read from this XML drives different Activities of the application. Presently, I have called the "parsing and the properties-initialization" from the onCreate() of my Main Activity.

I have a few questions as regards this case/approach:

  1. Should I invoke the app initialization method from the Application Object or is the current approach correct? What advantages/ disadvantages do/would we get/have if I choose to invoke it from the Application object?

  2. Do we really need a static class to store app properties? Or can we have all the properties as a static Collection variable in the application object?

  3. Parsing a XML(~200 nodes) at app load time might take some time(not sure how long tho); How can I avoid the dreaded ANRs? I am using a Pull Parser.

Please help me find answers to these questions.

Thank you.

A: 
  1. It depends on what you're initializing. Application's onCreate() should be used when you're doing things that need to be done before any part of your app works correctly and only needs to be done once, whereas Activity/Service/etc's onCreate() should be used for things that are needed for that component alone and needs to be done multiple times.

    The main concern I have for putting all your initialization into a component is that it will make extending your application more difficult later on. Suppose you want to make some Activity in your application accessible by outside intents - now you've got to either move the initialization code to Application or you have to duplicate initialization code in the non-launcher Activity.

  2. It sounds like you should check out SharedPreferences, especially PreferenceManager.getDefaultSharedPreferences(). The preferences will be stored between sessions and it gives you easy access to simple properties from any Context.

  3. Threading. I find AsyncTask to be the easiest way to accomplish this task; there's a good write-up on it at Google. Alternatively, you could fire up a Service to do this in the background while having a foreground Activity inform the user that you're booting up the app.

Daniel Lew
A: 

The Application object is used for sharing non-persistent state across the application. I don't think you'll need to use an Application class at all. You can do your initialisation in the onCreate() method of the Activity that is called first. To quote the documentation:

The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.

You don't need to create your own class to store application properties. This is done for you by SharedPreferences.

You should also have a look at the setDefaultValues() method in the PreferenceManager class as this will set preferences from the data in an XML file. What's nice about this method is that use the readAgain parameter so that the XML is only parsed once - the first time you start up your application - rather than every time.

Dave Webb