0

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.

I know I need to use code like SharedPrefences and saving to SD-card/app memory.

Onhar
  • 3
  • 6

1 Answers1

0

Yeah, you should consider a simple state machine design.

Say you created an Enum, ERuntimeState, which has two components;

enum ERuntimeState { INITIALIZE, BEEN_INITIALIZED; }

You can view a useful example of saving data using SharedPreferences here, if you need a reminder. 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. 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.

Once this is done, you can use the ERuntimeState you've fetched to configure what contents you want to load on your screen; 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.

Of course, by defining this enum, we're not limited to just two states. 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.

Community
  • 1
  • 1
Mapsy
  • 4,192
  • 1
  • 37
  • 43