3

I need to get String from resources in some place where Context object not available. I can pass context object as parameter and get string from resources like this:

context.getString(R.string.some_string);

But i also can use My Application (extends Application) instance like this:

MyApplication.getInstance().getResources().getString(R.string.some_string)

Are there any disadvantages for second approach?

Lester
  • 2,544
  • 4
  • 27
  • 38

1 Answers1

1

You should use ApplicationContext when you want to make something global to all activities or to keep hold on something until user go out of your application.

Activity context hold on to objects as long as activity is alive and memory will be released ondestroy call of activity but if you have used application context then the object will stay inside memory, as long as your app is active and this can cause a memory leak.

In your case , it better to use activity context whenever possible for objects local to that activity specially when something is related with GUI components of activity.

Read this post for more detail

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68