title: Android: static field set to null
body: <p>I have an Activity with a private static field. And that static field is set to null when I turn the screen. Of course, one would guess that Android kills the process, but I'm sure it does not. But let's begin from the beginning.</p>
  
  <p>There is a JNI library whose functions may be called from only one thread. From Java, the library functions are visible as native methods of an object (by the way, only one instance of that object is meaningful, there will be only one instance of the library with its static structures). The library object is used via a wrapper, a Java layer providing read/write access locks. The activity has a private static field referencing that wrapped library object. The library performs a long operation on a separate thread.</p>
  
  <p>The singleton creation was like the following. Since all activities are created on the same UI thread, in onCreate() I just checked if the field is null and if it is null, created a wrapped library object. There was no problem -- at least, on Android 2.x.</p>
  
  <p>Now, on Android 4, the following happens: I start a long operation and turn the screen.
  The activity is re-created, the savedInstanceState parameter in onCreate() is not null, but that private static field <em>is</em> null.
  Now, a new library object is created, then a new wrapper is created with its new read/write lock, and then a library operation is invoked... of course, the library is still busy with the request initiated before turning the screen, the new read/write lock does not protect the old library, data get corrupt and everything crashes.</p>
  
  <p>I have heard that a remedy to this would be to keep the reference not as a static field but as an instance field of the Application.</p>
  
  <p>Now, the question is:</p>
  
  <p><em><strong>WHY?</em></strong> <em><strong>What the hell is going on?</em></strong> <em>Do I have to rewrite all singletons?</em></p>