views:

42

answers:

2

I'd like to extend Application in my Android app. I've done this such that I've created an extended Application object called MyApplication and added it to the manifest.

I'd now like to add some getters and setters to hold some information. It looks like I'll need to pass the application Context to any classes which do not contain a Context.

For example, say I create a class called MyObject (in its own java file):

public class MyObject {
  public void doStuff() {
    // do stuff
  }
}

How might I access MyApplication from doStuff()? It looks like I'll need to pass the application Context to MyObject. Is this typical? Is there a possibility of leaks?

Also, to make sure I'm clear, will MyApplication (and the variables within) live throughout the application's lifecycle, or not? Either way is fine. I just want to make sure I account for this if I need to.

And lastly, any links to some example source to show what different things extending Application is useful for would be appreciated.

+1  A: 

As far as I know, passing along contexts is not exceptional in Android development. Well, either that or I'm doing it wrong.

I'm not sure if you needed this answered or not, but from a context, you can access the Application object like this:

((MyApplication) context.getApplicationContext()).getMyGlobalVariable();

Your MyApplication class should live throughout the entire application lifecycle, yes.

Also check out this answer.

benvd
Thank you for your response. I do most of my heavy lifting in an IntentService (where I have access to the application's Context) and most other calls to my global data will be from within Activities; so I think I'll be ok. Any other places (like in my OP example), I can just pass the data.
Andrew
+1  A: 

Passing along Context is fine in Android as long as you pass the correct context. Avoid passing down Activities as context. Use getApplicationContext() instead.

Your Applicationobject and all the data associated with it will persists through the whole life of your application. Even if the app is in background and all the activities get cleared from memory because they are not shown anymore. At what time the application object will be cleared from memory is not very well documented but I think that if your app is in the background and memory is rare the OS may destroy your whole app and therefore also removing the application object from memory.

There is no way to get the application object if you can't access a context. If you only need a reference to the application just pass down the application object, this will minimize the errors that can arise form saving a reference to an activity that is not on screen anymore and leaking the whole activity through this reference.

I don't have any example code but I used it to hold references to some tasks. This enabled me to allow the user to change between activities very fast without making her wait for the tasks to return even if the next activity has to change some states depending on the tasks that are running at the moment.

Janusz
Just a note regarding passing along getApplicationContext(). Don't use it to launch an intent to open a web page -- it'll throw an exception. :)
benvd