0

In my app Application class is overrided and used for storing some data. Where needed I'm using ((MyApplication)getApplication()).someMethod() and it working well. But I have one class where Activity is not available. In this class is available the context only. So I cannot call method getApplication().

How can I get Application object without getApplication() method? May be I can get it from Context or via static methods?

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
BArtWell
  • 4,176
  • 10
  • 63
  • 106
  • If you have context object in your class ,you can call context.getApplication(); – Anand Phadke Apr 02 '14 at 11:40
  • This sounds like you should not be using a subclass of `Application`, but rather a regular Java static data member. – CommonsWare Apr 02 '14 at 11:41
  • @andrewww, are you sure? I cannot find getApplication() method here, only getApplicationContext() or getApplicationInfo(): http://developer.android.com/reference/android/content/Context.html – BArtWell Apr 02 '14 at 11:45
  • @CommonsWare, as I know, regular singleton class in Android can be removed when the activity restarted. – BArtWell Apr 02 '14 at 11:48
  • @CommonsWare, I tell about this: http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables – BArtWell Apr 02 '14 at 11:52
  • "as I know, regular singleton class in Android can be removed when the activity restarted" -- no, it is not. Static data members have the same lifetime as the `Application` instance, which is the lifetime of the process. – CommonsWare Apr 02 '14 at 11:54
  • you can cast context to Activity and can call getApplication() method – Anand Phadke Apr 02 '14 at 12:00

0 Answers0