getApplication() for preferences, how to access...

Put your problem here if it does not fit any of the other categories.

getApplication() for preferences, how to access...

Postby SwizzerSweet » Thu Jun 03, 2010 3:21 pm

I'm trying to get some global preferences for my Android app, so I've started out by adding my own class that extends Application.

Code: Select all
public class myApplication extends Application {
   private SharedPreferences settings = null;
    public void onCreate() {
       //get preferences
       settings = getSharedPreferences("myApplicationPreferences",0);

    public SharedPreferences getSettings() {
       return settings;
    public void setSettings(SharedPreferences sp) {
       settings = sp;
     * Called when Application loses focus or leaves (reliable)
    public void onPause() {

Now I'm told it's accessible through getApplication() from any activity

Code: Select all
//any activity

      //get application ( (code above)
      myApplication = this.getApplication();
      if   (myApplication != null) {
         //this code runs, meaning I've got the Application object

So now myApplication contains a reference to the Application (because the code runs in the brackets), but my question is, how do I acesss methods and properties from the Application? I keep getting errors. If someone could show me some code on how to do both properties and methods, it would be greatly appreciated.


Once Poster
Once Poster
Posts: 1
Joined: Thu May 27, 2010 4:49 pm


Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot], MSNbot Media and 10 guests