patbenatar wrote:
> I am using an Application object to store app-wide instance data--data
> that I need to remain in existence for the entire lifetime of the app,
> so the Application class seems like the proper place to put it. 

A database, flat file, or other persistent store is the proper place for
it, perhaps cached via a custom Application class or static data members.

> I have
> just finished reading this thread
> http://groups.google.com/group/android-developers/browse_thread/thread/502fe58d8ed44102#
> that goes into pretty good detail and receives confirmation from
> Googler hackbod as to how the Application class performs--and from
> that info it seems this strange behavior I am receiving should not be
> happening...

Bear in mind that the thread in question is over two years old. Anything
posted before August 2008, in particular, should be viewed with some
skepticism.

> Basically, every once in a while when I resume my app in any of its
> Activities (it is still running in memory so onCreate is not called) I
> get NullPointerExceptions because instance variables in my Application
> class are returning null when I request them from my Activity (via a
> getter method in my custom Application class). It seems as if
> sometimes Android saves the states of my Activities but deletes all my
> instance variable data... This can't be an Android problem--there must
> be something wrong with my code.

I suspect that the problem is that you're assuming that "the entire
lifetime of the app" is longer than it is.

> The error is most easily re-produceable when I am in Activity B, after
> A started it for result, and then click a button in B to launch Maps
> with an Intent to search for an address, then from Maps I hit Home
> then my launcher icon on my Home Screen to go back into my app--BAM!
> Activity B fails onResume with a NullPointerException coming from a
> getter method on my MyApplication class, when I know for sure Activity
> A properly set the data in MyApplication.. 

It is entirely possible that your activities and application were
destroyed while you were in the Maps and Home applications, to free up
memory.

You cannot assume that when your activity starts up that the application
has already been around, even if you are getting savedInstanceState.
Using a custom Application or static data members as a cache is
perfectly fine, but you cannot reliably use it to hold "app-wide
instance data". You *can* use it to cache "app-wide instance data" that
is backed by a persistent store (database, flat file, "the cloud",
whatever).

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android App Developer Books: http://commonsware.com/books

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

Reply via email to