简体   繁体   English

不同的一次性屏幕

[英]Different One-time screens

I am Wondering about one-time screens. 我想知道一次性屏幕。 I'll need to use few one-time screens on my app. 我需要在应用程序上使用一些一次性屏幕。

First of all: 首先:

One-time screen, that SAVES user selection from 3 options (I use image buttons) and let him go further, but next time user comes to this activity, it opens the activity of the option that user selected earlier. 一次性屏幕,该屏幕可以保存3个选项中的用户选择(我使用图像按钮),然后让他走得更远,但是下一次用户进入此活动时,它将打开用户先前选择的选项的活动。

I know I need to use code like SharedPrefences and saving to SD-card/app memory. 我知道我需要使用SharedPrefences之类的代码并将其保存到SD卡/应用程序内存中。

Yeah, you should consider a simple state machine design. 是的,您应该考虑一个简单的状态机设计。

Say you created an Enum , ERuntimeState , which has two components; 假设您创建了一个Enum ERuntimeState ,它具有两个组件;

enum ERuntimeState { INITIALIZE, BEEN_INITIALIZED; }

You can view a useful example of saving data using SharedPreferences here , if you need a reminder. 如果需要提醒,可以在此处查看使用SharedPreferences保存数据的有用示例。 When using SharedPreferences , you're able to query an existing storage flag for your application, and if a variable hasn't been set before, you can set it's default value. 使用SharedPreferences ,您可以查询应用程序的现有存储标志,并且如果之前未设置变量,则可以设置其默认值。 So, when the application first runs, you can use SharedPreferences to check for a saved instance of ERuntimeState , using INITIALIZE as the default that will be picked if there's no existing save data. 因此,在应用程序首次运行时,可以使用SharedPreferences来检查ERuntimeState的已保存实例, ERuntimeState使用INITIALIZE作为默认值,如果不存在现有保存数据,则将使用该默认值。

Once this is done, you can use the ERuntimeState you've fetched to configure what contents you want to load on your screen; 完成此操作后,您可以使用获取的ERuntimeState来配置要在屏幕上加载的内容。 this is where you can display your first-time launch interface. 在这里您可以显示您的首次启动界面。 We have to make sure that once this state is entered, you use shared preferences to save an ERuntimeState value of BEEN_INITIALIZED , so you don't execute the initialization step again on your next run. 我们必须确保一旦输入此状态,就可以使用共享首选项保存ERuntimeStateBEEN_INITIALIZED ,这样就不必在下次运行时再次执行初始化步骤。

Of course, by defining this enum , we're not limited to just two states. 当然,通过定义此enum ,我们不仅限于两个状态。 Otherwise we'd just use a boolean, and that's inflexible. 否则,我们将只使用布尔值,这是不灵活的。

As an aside, this might be quite hard to debug! 顺便说一句,这可能很难调试! You might want to make sure you don't save the flag in your initialization state whilst you're debugging, otherwise you'll have to keep clearing all of the application's cached data in order to emulate an initial launch. 您可能要确保在调试时不要将标志保存在初始化状态,否则,您将不得不清除所有应用程序的缓存数据,以模拟初始启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM