[Monodroid] How to load Assets outside of an Activity?
Amir Waldman
amirw2k at gmail.com
Tue Mar 1 09:44:02 EST 2011
Thanks for the detailed explanation Jon,
I ended up doing it the 'bad way' because I need to have my code
disconnected from the 'Android Way' to make it compatible with 'Other Ways'
too :-)
To make things short, I had to pass a reference to the main activity as a
global parameter only to load assets.
Regards,
Amir
-----Original Message-----
From: monodroid-bounces at lists.ximian.com
[mailto:monodroid-bounces at lists.ximian.com] On Behalf Of Jonathan Pryor
Sent: Tuesday, March 01, 2011 4:02 PM
To: monodroid at lists.ximian.com
Subject: Re: [Monodroid] How to load Assets outside of an Activity?
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 WayT. 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
_______________________________________________
Monodroid mailing list
Monodroid at lists.ximian.com
UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
More information about the Monodroid
mailing list