首次运行应用程序时显示首选项屏幕和相关问题
我有一个有 2 个活动的应用程序,即首选项和主要活动,我需要首选项屏幕在应用程序首次运行时显示,以便用户可以进行一些配置。我已经检查了有关此主题的答案,它们似乎不太清楚,但我认为这与检查共享首选项文件是否为空有关。
有人可以给我一个代码来解决这个问题以及我应该将代码放在哪个活动上吗? 另外,我仍处于开发阶段,因此我已经设置了偏好设置,如何撤消此操作?
提前致谢
I have an app with 2 activities, the preference and the main activity, I need the preference screen to show first time the app is run so the user can do some configuration. I have checked through the answers on this topic and they don't seem very clear but I gather it has to do with checking that there are sharedpreference file is empty.
Can someone please give me a code to sort this out and on which activity would I put the code?
Also I am still in the developing stage so I already have my preferences setup how do I undo this?
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
1) 当您的主要活动开始时,检查布尔首选项,默认设置为 false。如果为假,则启动您的偏好活动,如果为真,那么您就知道您已将其保存为真!
2) 在您的首选项活动中,在
onCreate
中保存具有true
值的相同布尔首选项1) When your main activity starts check a boolean preference with the default set to false. If it is false, launch your preference activity, if it is true then you know you have saved it to be true!
2) In your preferences activity save the same boolean preference with a value of
true
inonCreate
我假设您正在运行模拟器,当您启动模拟器时,您可以选择在启动模拟器时“擦除保存的数据”,这样就像您刚刚启动应用程序一样启动它。或者,您可以进入设置 ->应用->您的应用程序->擦除数据。
关于您的编码解决方案,我目前没有任何方便的东西,但您应该做的是启动主要活动,运行过程/函数来检查共享首选项文件是否为空,以及是否启动首选项活动,否则运行您的主要活动。或者,您可以查看正在查找的用户输入值(例如 UserID)是否为空,而不是检查文件是否为空。如果该值不为空,则意味着应用程序可以继续。
I assume that you're running an emulator, when you start the emulator you have the choice to "wipe saved data" when you start it so it will be like you started it as if you just started the application. Alternatively, you can go into settings -> Applications -> You app -> Wipe data.
In regards to your coding solution, I don't have anything handy at the moment, but what you should do is start your main activity, run a procedure/function to check if the sharedpreference file is empty, and if it is start the preference activity, otherwise run your main activity. Alternatively, instead of checking for the file to be empty, you could see if a value that you are looking for user input (for example UserID) is null or not. If that value isn't null that means that the application can continue.
我已经在我的主要活动中用这段代码解决了这个问题,
它只是检查您的一个值是否为空,但您需要将其放在 onCreate 的底部,否则当您返回主页时它将是空白的。
I've sorted this out with this bit of code in my main activity
It just checks if one of your values is empty but you need to put this at the bottom of onCreate or else when you go back to the main page it will be blank.
我正在做这样的事情。它对我有用。
I am doing something like this. And its works for me.