Tobiah,

13.12.2010 22:56, Tobiah пишет:
I want a handle to my sqlite database to be available to many activities.
I read about subclassing Application, and storing things there. Then I
should be able to get them through getApplicationContext(). An article
explaining this says that I must "specify that class in the application
tag in your manifest". What would that <application> tag attribute look
like?

http://developer.android.com/guide/topics/manifest/application-element.html

<application name="your_application_class_here">


Also, in the docs for android.app.Application, this is said:

There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

I don't really understand this, although I know what a singleton is.

There can many contexts in an Android application. An Activity is a Context, so is a Service.

A singleton should not keep a reference to any context that might go away before the singleton does (i.e. never), or else you'll get a memory leak.

That's why, if the singleton needs to call a method that requires a Context, or needs to keep a reference to Context, it should call getApplicationContext() on any Context that's passed in. This avoids excessive references from the singleton to an Activity or Service. You don't actually need to subclass Application for this.

The Application object is there for the duration of the process, so having one more reference to it from the singleton isn't going to ruin anything.

Note that the only thing really you need a Context for, with respect to databases, is to give to your subclass of SQLiteOpenHelper, so the base class can call Context.getDatabasePath.

You might also take a look at implementing your own (private to your application) ContentProvider. It's quite convenient.


Also, if android reclaims my apps memory after a long period of disuse,
does the app actually have to start up again from scratch when it comes back?
Can there be no static state through that process?

Once a process is killed, it's gone forever :) and will be recreated from scratch for next launch.


Thanks,

Tobiah



--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to