Yep. The JVM is killed. When restarted later, only "OtherActivity" is instantiated (even though MainActivity is necessarily loaded, in order to provide access to its statics). So the "OnCreate" never runs for "MainActivity".
If the desire is to maintain this same basic pattern, the initialization of "lib" should be done in the "static" constructor of MainActivity, since the static constructor must be executed before any other classes can reference its static variables. On Aug 6, 7:18 am, Dianne Hackborn <[email protected]> wrote: > If the app is killed, the next time the user returns to it only the visible > activities at that point will be re-started; not the entire activity stack. > > > > On Fri, Aug 6, 2010 at 4:48 AM, Michael A. <[email protected]> wrote: > > I am seeing a lot of NullPointerException in my code at the moment > > from public static variables. > > > I use these variables to store, e.g., my image library (used in > > Activities throughout my code). All of them are initialized in the > > OnCreate of my apps Base activity, and I check that they are > > initialized in OnResume just to be certain (and initialize them if > > they have somehow become nulled). > > > // Main APP Activity > > MainActivity { > > public static GlobalObject lib; > > > void OnCreate() { > > if (lib == null) > > lib = new GlobalObject(); > > } > > > void OnResume() { > > if (lib == null) > > lib = new GlobalObject(); > > } > > > } > > > // Some other activity instantiated later > > OtherActivity { > > void onCreate() { > > lib.doSomething(); // This line will sometimes fail with a > > NullPointerException > > } > > > } > > > I am quite unable to recreate the situation in which this > > NullPointerException occurs. As I understand the activity lifecycle, > > it should not be possible for lib to be null when OtherActivity is > > created (if OtherActivity is in memory, MainActivity's static > > variables should also be in memory and if the app is killed, it should > > start over from MainActivity?), but that is the error I seem to be > > getting. > > > Suggestions, anyone? I suppose I am probably overlooking something > > simple, but I just can't see it. > > > Regards, > > > Michael A. > > > -- > > 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]<android-developers%[email protected]> > > For more options, visit this group at > >http://groups.google.com/group/android-developers?hl=en > > -- > Dianne Hackborn > Android framework engineer > [email protected] > > Note: please don't send private questions to me, as I don't have time to > provide private support, and so won't reply to such e-mails. All such > questions should be posted on public forums, where I and others can see and > answer them. -- 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

