Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Application Initialization

Tags:

android

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.

like image 283
Samuh Avatar asked Dec 21 '09 08:12

Samuh


People also ask

How do I change the startup programs on my Android?

Androids can vary significantly based on the manufacturer and the model. To give this method a try, open Settings and go to the Application Manager. It should be in "Installed Apps" or "Applications," depending on your device. Select an app from the list of downloaded apps and turn the Autostart option on or off.

What is AndroidX startup?

AndroidX App Startup is a recent addition to the Android Jetpack suite of libraries. It provides a performant* way to initialize components at app startup by avoiding the use of a separate ContentProvider for each library, thus eliminating the setup cost that comes with each of them.

What is init Android studio?

Initializer s can be used to initialize libraries during app startup, without the need to use additional android. content. ContentProvider s.


1 Answers

  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.

like image 102
Dan Lew Avatar answered Sep 29 '22 06:09

Dan Lew