static variable null when returning to the app
This is standard behavior in most mobile operating systems, definitely including Android. Your app is in fact very often killed if some other application with higher priority (generally, if it's in the foreground it's higher priority) needs the resources. This is due to the nature of mobile devices having relatively limited resources.
You should save your data somewhere more durable. You might find this article on general Data Storage to be useful. This question should be relevant too: Saving Android Activity state using Save Instance State
Note that this is in fact not a one-out-of-six device problem. This is a "problem" on all devices, it's just more apparent on one of your devices probably because it has less memory. If you run a very memory-intensive app on any of your other devices you should see the same behavior. Also there is no flag to prevent this. This is standard and expected.
Static variable returns null when returning to the app
Couple of points:
1- static
is not final
and therefore can be null, in other words, make sure you are setting those values to something.
2- super.onRestoreInstanceState(savedInstanceState);
should be called at the start of protected void onRestoreInstanceState(Bundle savedInstanceState)
not end of it.
Source (Look at the end of the page)
Let me know, cheers.
Static objects becomes null when app brought back after being in background for long time
I would like to suggest to put the login information in the SharedPreference
and then read the login data from preference inside your onResume
function. This way the application will fetch the data from SharedPreference
each time and the value stored in SharedPreference
is saved.
However, you should not lose your static value if the app is not closed. I would suggest checking if there is any other life-cycle method (e.g. onDestroy
) where you have assigned the value null
in your static variable which might raise the problem.
Why static variable of my android application sometimes return null?
Don't use static variables, its bad practice. Android can destroy you application's process at any time and later recreate it by recreating activities as necessary. If you're creating your static variable in the "first" activity then it wont get initialized when the 2nd or 3rd are started first.
Java Static Variable becomes null
This generally would happen if the user lets the phone go to sleep and the system requires or clears memory. It's best to keep information that you would require over a longer duration into a disk cache rather than just keeping it in a static variable. As you would know that the Android system has the final say on when to clear an app, only keep data that you would need for a very short interaction in a static variable.
Related Topics
Listview with Onitemclicklistener
How to Set Android Layout to Support All Screen Sizes
How to Apply a Style to All Buttons of an Android Application
How to Enable Tls 1.2 Support in an Android Application (Running on Android 4.1 Jb)
How to Save Hashmap to Shared Preferences
How to Create Turn-By-Turn Gps Navigation App on Android/iOS Using Google Maps
Android Ics and Mjpeg Using Asynctask
How to Change Package Name of Android Project in Eclipse
Convert a Bitmap to Grayscale in Android
How to Turn Off 3G/Data Programmatically on Android
Convert Content:// Uri to Actual Path in Android 4.4
How to Create a Notification with Notificationcompat.Builder