eehouse wrote:
> Are there any scenarios on Android where a process can be instantiated
> (classes loaded, etc.) and a non-main activity launched without going
> through the main activity?

Sure. The process may have been terminated to reclaim memory, then
reconstituted when the user, say, BACK-buttons to the activity.

Mutable static data members should be avoided where possible, used in a
very transient fashion if needed, or at most used as a non-essential cache.

> Background: my app has a single main activity
> (android:name="android.intent.action.MAIN" and
> android:name="android.intent.category.LAUNCHER"), some secondary
> activities, and a preferences class providing static methods meant to
> be called from anywhere.  These static methods' implementations
> require a Context, and so the class has a static Context member
> s_context; the static methods assert that s_context is non-null before
> using it.  

That is supremely dangerous, as you can introduce a substantial memory
leak. Not to mention it is causing the problem you are experiencing.
Please just pass in a Context to the static helper methods.

> The very first thing I do in my main activity's onCreate()
> method is pass getApplicationContext() into the preferences class so
> it can set s_context.

See above, plus there is no need to use getApplicationContext() ever,
hardly. An Activity is a Context. A Service is a Context.

> The main activity launches a secondary activity that calls the
> preference class's static methods.  Sometimes -- never in an emulator,
> and only after my app's been running for a while and moved to the
> background several times -- those asserts fail.

This is not surprising. Please just pass in a Context to your static
helper methods.

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

Android Online Training: 21-25 June 2010: http://onlc.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