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

bgn
bgn
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.

Thanks!

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

Share:   fb twitter linkedin
Answers
Smith
Smith
Participant
374 Points
112 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
                            .getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = settings.edit();
editor.putString(some_key, your_auth_token_string);
editor.commit();

To get the token

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