Reputation: 1034
I am using static variable in a class in Android:
class Test{
static int x;
}
This variable is deleted when the class is unloaded or when the VM stops.
I need to keep this variable for a longer time period, but I can't use SharedPreferences or any permanent storage because the information in this static field is sensitive and should not be stored in any accessible storage.
Any suggestions?
Edit:
As the Android service is run in background and is less to be stopped by the OS, I decided to set the static field inside a Service class, but strangely this does not solve the issue, it seems OS will unload the Service class even if the service itself is still running in background, or at least it seems it is loading it again when some event is called, I do not know exactly what is going on but the static field becomes NULL after a period of time as I set a notification in a timer to check for that. anyone can confirm this?
Upvotes: 1
Views: 226
Reputation: 696
You could create a started service that hangs around after the activity is destroyed. The service can be destroyed when no longer needed. You still need to follow good practices, private variables public getters and setters and be careful in the manifest etc. Static in this case means you only have one copy of the value of the variable regardless of the number of objects of the test class type.
Upvotes: 1
Reputation: 401
If you want to store sensitive information(key or token) you can use keystore provider , please find the android developer link - http://developer.android.com/training/articles/keystore.html . but it is available from android 4.3 onwards .
Upvotes: 1