Android:仅一次变量存储

发布于 2025-01-07 20:11:27 字数 379 浏览 5 评论 0原文

我有一个应用程序,仅在用户第一次启动该应用程序时才会显示欢迎窗口。将此布尔变量(即“isFirstTime”)存储到手机存储的最佳方法是什么?我应该使用共享首选项还是内部存储

文档说,如果我使用内部存储,我的“首选项”文件将在卸载时自动删除,这非常方便。

我想要一个干净、简单且快速的解决方案。

I have an appliaction which will show a welcome window only the first time the user starts the app. What would be the best way to store this boolean variable (i.e. "isFirstTime") to phone storage? Should I use Shared Preferences or Internal Storage?

The docs say that if I use Internal Storage my "preference" file will automatically get removed upon uninstallation which is quite handy.

I want a clean, simple and fast solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一杆小烟枪 2025-01-14 20:11:27

是的,我建议使用共享首选项。基本上,您可以将共享首选项的键设置为“isFirstTime”,并将布尔类型设置为 false。然后在您的主要活动中执行以下操作:

getBoolean (isFirstTime, true);  

如果找不到 isFirstTime 将为您提供 true,允许您根据结果执行 if 语句。

Yes, I'd recommend using Shared Preferences. Basically you could put a shared preference with a key of "isFirstTime" and a type of boolean set to false. Then in your main activity do something like:

getBoolean (isFirstTime, true);  

This, if it can't find isFirstTime will give you true, allowing you to do an if-statement based on the result.

吹泡泡o 2025-01-14 20:11:27

我同意 SharedPreferences 可能是您正在寻找的最“干净、简单和快速的解决方案”。卸载应用程序时,SharedPreferences 也会被删除。

是共享首选项删除应用程序时与该应用程序关联的删除?

I agree that SharedPreferences would probably be the most "clean, simple, and fast solution" that you are looking for. SharedPreferences are also deleted when the application is uninstalled.

Are the shared preferences associated with the App deleted when the app is removed?

香草可樂 2025-01-14 20:11:27

SharedPreferences 是您的最佳选择。

SharedPreferences are your best option for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文