Where should I put global methods and variables in an Android app? -
when i'm writing method or using member variable, find need share them across app. should go?
i can subclass activity, falls on use mapview , forced use mapactivity, not activities inherit subclass. there way around this?
where inheritance isn't applicable, tending put generic methods , member variables subclass of application object, i'm finding it's creating mess of code every class needs either grab access application object through via context, or have pass down.
i suspect better off creating myapplication.getinstance() , keeping in singleton, instead of passing application object down through app classes. before wanted see guys had say.
if want access "global singleton" outside of activity , don't want pass context
through involved objects obtain singleton, can define, described, static attribute in application class, holds reference itself. initialize attribute in oncreate()
method.
for example:
public class applicationcontroller extends application { private static applicationcontroller _appctrl; public static applicationcontroller getappctrl() { return _appctrl; } }
one example resources: because subclasses of application
can obtain resources, access them when define static method, returns them, like:
public static resources getappresources() { return _appctrl.getresources(); }
Comments
Post a Comment