Comments (4)
你这是单例了,这样sp的name不能修改了
from androidutilcode.
怎么说呢,代码量大到一定程度后,自己写的也不敢确定sp的name在哪里改了,所以现在这样 我要是用的话在get和put之前肯定会先初始化一下sp的name,但这样又很麻烦,不这样心里不放心。
from androidutilcode.
建议这样,大多数用一个单例的sp,有特殊保存比如token还是自己重新封装好:
// 如果需要一个单独的SharedPreferences来保存某些数据,例如:token,可以这样:
private static final String PRE_TOKEN = "token";
private static SharedPreferences getInstance(Context context, String preferenceName) {
if(sp == null) {
sp = context.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
}
return sp;
}
public static void putToken(Context context, String token) {
sp = getInstance(context,PRE_TOKEN);
sp.edit().putString(PRE_TOKEN, token).apply();
}
public static String getToken(Context context) {
sp = getInstance(context,PRE_TOKEN);
return sp.getString(PRE_TOKEN,"");
}
from androidutilcode.
保存特殊的只需要改name就好了挺方便的,单例的话就修改不了,还要重新开一个单例,其实单例只是为了复用上次存在的值,而这个单例的区别就是getSharedPreferences这一个步骤,你是直接读取上次的sp,而我是调用getSharedPreferences获得,性能基本没区别啦,考虑到使用方便就不单例了
from androidutilcode.
Related Issues (20)
- 使用ToastUtils时导致App闪退 HOT 2
- TimeUtils切换语言时会有bug
- 未经用户允许提前获取用户androuid id HOT 5
- 似乎没有适配Gradle插件8.1,Gradle 版本8.0 HOT 1
- ToastUtils在安卓13上的问题
- utilcodex:1.30.4 工控屏 rk3288 Android 7.1
- 是不是已经停止更新了
- wifi热点api
- LogUtils能否添加一个回调? HOT 3
- NetworkUtils安全检测到动态注册广播风险
- NetworkUtils的isAvailable方法,在热点的数据网络关闭的情况下,会返回错误
- H4ro from z4ro HOT 1
- 关闭屏幕 HOT 1
- 现在进群会不会太晚🤨🤨
- RObbOT
- 混淆后的一个错误
- 在Activity中使用ToastUtils后finish当前activity,控制台会提示内存泄漏,用原生Toast.maketext就不会是咋回事呢? HOT 1
- 不支持jdk11 吗
- 设备熄屏后,等待较长时间,然后亮屏,出现ANR的问题
- 怎么获取当前wifi连接的设备数?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from androidutilcode.