I have an android application that is running. After a while when user quits the application by running something else, and returning to my app, the static variables in the application is seem to have been garbage collected.
In a nut shell, I’m keeping the entered username/password at startup of the application and keep them in a static variable, and use them for communication with server. I either need to find out when they are garbage collected at application re-launch (so that I redirect them to login view) or prevent this class from being garbage collected. Ideas?
One way you could implement your second scenario is by implementing your own class that inherits Application, and specify it in your manifest. You can put your static variables in that class. Android will create one instance of that class when it launches your process, and that instance will be alive as long as the process is alive too.
So, if you have a simple boolean in that class that denotes if a signin has been performed, you now have a reliable way to check at any point whether you should direct the user to the login activity, or try using the in-memory username/password.
In addition, you could use one of the standard Android persistence component (shared preference file, SQLLite, AccountManager, OBB, credential storage, etc) to persist the credentials across process restart. Note however, that doing so raises a whole new set of issues around how to properly secure that persisted copy of the user credentials, in order to protect it from unauthorized access by other applications (especially on rooted phones).