[Monodroid] How to load Assets outside of an Activity?
Jonathan Pryor
jpryor at novell.com
Tue Mar 1 09:01:33 EST 2011
On Feb 28, 2011, at 6:40 PM, Amir Waldman wrote:
> I'm trying to load assets outside of an Activity.
You don't need an Activity. You need a Context...which isn't a huge improvement, but does allow using Android.App.Application, Android.App.Service, Android.App.Backup.BackupAgent, as well as Android.App.Activity.
I believe that Android's intent is that you already have a Context instance "somewhere" (the starting Activity, Service, etc.), and thus you should pass that instance around to whatever code needs it.
> This is what I have:
> new Android.App.Activity().Assets.Open(...)
You don't say if that works, but I _suspect_ (without testing) that it won't work, as Activity's constructor calls the ContextThemeWrapper constructor which passes 'null' as the base context for the ContextWrapper constructor, and ContextWrapper.Assets calls `mBase.getAssets()`, so this looks like it'll generate a Java.Lang.NullPointerException...
> Seems like you can instantiate AssetsManager like this, but it requires a
> pointer:
> new Android.Content.Res.AssetManager(???).Open(...)
The (IntPtr) constructors exist for JNI interop scenarios, as mentioned in:
http://lists.ximian.com/mailman/private/monodroid/2011-February/003259.html
> Is there a simpler, less memory consuming for this action? Why is it limited
> to Activity anyway?
It seems that passing around Context's to code that needs them is The Android Way™. Rephrased slightly, "global state is bad, mkay," and Android is adamant about removing implicit dependencies (hence all View objects taking a Context instance for a constructor parameter, and thus no default constructors, etc.).
- Jon
More information about the Monodroid
mailing list