java - Android passing Context everywhere -


i've developed android app (for me) it's ugly , i'm pretty sure approach wrong.

i have bunch of fragments activities , lot of classes async-tasks, business rules , on. in special, have class called propertiesreader use read properties file. use class in lot of places fragments , on business rules.

public class propertyreader {      private properties properties;      public propertyreader(context context){         super();         try {             properties = new properties();             properties.load(context.getresources().getassets().open("badass.properties"));         } catch (ioexception e){             log.e("error", "error opening properties file", e);         }     }      public string getvalue(string key){         return properties.getproperty(key);     } } 

in every place use class, like:

 propertyreader bla = new propertyreader(this); //or new propertyreader(context); 

i wondering best approach work classes need context constructed. in opinion, it's ugly every constructor has context parameter.

any ideas?

thank in advance.

create singleton, , save application context on creation.

it this:

public class propertyreader {      private static propertyreader ourinstance = new propertyreader();     private context mcontext;      public static propertyreader getinstance() {          return ourinstance;     }      private propertyreader() {     }      public void loadproperties(context context) {          mcontext = context;          try {              properties = new properties();              properties.load(context.getresources().getassets().open("badass.properties"));          } catch (ioexception e){              log.e("error", "error opening properties file", e);          }     }  } 

when application starting can like:

propertyreader.getinstance().loadproperties(getapplicationcontext()); 

and can access propertyreader everywhere else:

propertyreader.getinstance().getvalue(key); 

Popular posts from this blog