2

How can I register data (like integer or poco objects) shared by all activity like the id of the user ? Have I to use a simple singleton or is there a special Android way ?

Note : I don't need to make that data persistant (no need of SharedPreferences or sqlite)

Thank you

P. Sohm
  • 2,628
  • 2
  • 39
  • 67

2 Answers2

1

You can create your own class that implements Application and specify this in your manifest file. In that case, every time you call getApplicationContext you will get a reference of your application that can hold any kind of information.

Sample code:

class MyApplication extends Application {
  public void setMethod() {
    //
  }
}

((MyApplication)getApplicationContext()).setMethod()
Community
  • 1
  • 1
Tomislav Markovski
  • 11,818
  • 4
  • 43
  • 69
1

The android way is to create a custom Application for your project. Then in onCreate of that application you initialize whatever you need, and for example from an Activity do something like:

((MyApplication) getApplication()).getMyData()

If using roboguice you can use a @Singleton injection which basically does the boilerplate of a singleton for you - that's much nicer.

Paweł Obrok
  • 21,259
  • 8
  • 72
  • 67