Where is the best way to store globals (authentication token) in Android

I am new to Android. I want to know where it is best to store something like the authentication token that I get from entering my server. With each subsequent request, I will have to publish this authentication token. Of course, I could store the global class somewhere, but I just want to know what a good / standard way to store data that is stored in activities and intents . Thanks!

+8
android authentication access-token
source share
1 answer

SharedPreferences is the way to go. See the Doc here: https://developer.android.com/reference/android/content/SharedPreferences.html

Sample code as shown below.

To save the token:

 SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); SharedPreferences.Editor editor = settings.edit(); editor.putString(some_key, your_auth_token_string); editor.commit(); 

To get a token:

 SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); String auth_token_string = settings.getString(some_key, ""/*default value*/); 
+11
source share

All Articles