Where is the best place to store global (auth token) in Android

2 Points
1 Posts

I'm pretty new  android developer. I want to know where is the best place to store something global data  like a auth token that I get from logging in on API server. With every subsequent request, we need to post this auth token. Sure I could keep a global class somewhere, but I just want to know what a good / standard way is to store data that persists across activities and intents.


Views: 9775
Total Answered: 1
Total Marked As Answer: 0
Posted On: 11-Aug-2018 02:41

Share:   fb twitter linkedin
858 Points
169 Posts

You can use SharedPreferences to store the global data. See doc here :https://developer.android.com/reference/android/content/SharedPreferences

Following are few example of uses

To Save the token:

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

To get the token

SharedPreferences settings = PreferenceManager
String auth_token_string = settings.getString(some_key, ""/*default value*/);
Posted On: 18-Aug-2018 05:33
 Log In to Chat