0

I realize there is a very interesting technique, to get Context in Android in an easy way.

Static way to get 'Context' on Android?

However, I also came across

Android : Static variable null on low memory

Is it possible that the static variable used to hold context, become null during low memory?

Community
  • 1
  • 1
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • first question is why you want to make it static as we prerfer not to make static .............. – Dheeresh Singh Jun 24 '12 at 18:43
  • @DheereshSingh If you read the question at the first link, you will understand why we need it to be static. Is all about convenient. – Cheok Yan Cheng Jun 24 '12 at 18:46
  • "I realize there is a very interesting technique, to get Context in Android in an easy way." ... means no real issue just for ? only – Dheeresh Singh Jun 24 '12 at 18:51
  • @DheereshSingh This is an issues. Imagine you have 100 non-Activity classes. All those classes need to access to method, which require Context as parameter. It is pain to have to pass Context around to those 100 non-Activity classes. – Cheok Yan Cheng Jun 24 '12 at 18:55
  • 1
    in this case only if you calling all these function from activity class then I 'll prefer to pass the context if function are non-static'll pass in constructor and if function are static 'll pass in each rather then taking the chance by violating standered.... – Dheeresh Singh Jun 24 '12 at 19:05

1 Answers1

4

Is it possible that the static variable used to hold context, become null during low memory?

No.

The process may be terminated to free up memory, which has the effect of making static variables null when your code is next run, in its new process.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • May I know why there are some developers encountered problem in 2nd link? Does it matter, if I place static variable within `Application` class scope, and place static variable within `Activity` class scope? – Cheok Yan Cheng Jun 24 '12 at 18:50
  • @YanChengCHEOK, the first link addresses a convenient means of retrieving the "application context" across the entire application. the application context is non-`null` during the application's entire lifecycle... so that is why it works in this particular case. If you need to store persistent data then you shouldn't be doing so by using static variables... you should be storing it in a database of some sorts instead. – Alex Lockwood Jun 24 '12 at 18:57
  • Subclassing `Application` to keep references to global static variables is OK because the `Application` class exists across the entire application's lifecycle as well. Nonetheless, you shouldn't use this as a means of storing persistent data either. – Alex Lockwood Jun 24 '12 at 19:00
  • 1
    @YanChengCHEOK: "May I know why there are some developers encountered problem in 2nd link?" -- their process was terminated, as I outlined in my answer. "Does it matter, if I place static variable within Application class scope, and place static variable within Activity class scope?" -- `static` is `static`, no matter where you place it. – CommonsWare Jun 24 '12 at 19:06