-1

So at the moment the user will select a colour, shape and time. When the app first starts up there is a default for each. What I want to happen is to keep the users selection for all 3 of these even after they close the app while it runs in the background.

If you need me to post anything to help ask away. Thanks.

After writing out what the problem was, it's not that they're not being saved it's the fact that the selections ARE being saved but not being displayed properly. I.E by a border or the dropdown menu results back to it's default value instead of the users selection.

So the values are being saved(my bad) but the app overwrites these saves and doesn't use the border or beings the dropdown menu back to it's default value.

 circularImageView = (CircularImageView)findViewById(R.id.activity_main_silver_color_button);
    circularImageView.setBorderColor(getResources().getColor(R.color.unselected_border));

circularImageView = (CircularImageView)findViewById(v.getId());
    circularImageView.setBorderColor(getResources().getColor(R.color.selected_border));

These work independent of each other but when the border is selected, if a another color was selected then it becomes an unselected border. But what happens if "selected_border" state is never saved.

public static void saveLockScreenDuration(Context context, int duration){
    getStoredPreferences(context).edit().putInt(SELECTED_DURATION, duration).apply();

public static int getLockScreenDuration(Context context){
    return getStoredPreferences(context)
            .getInt(SELECTED_DURATION, DEFAULT_DURATION);
}

This is where all the prefereces are stored. DEFAULT_DURATION and SELECTED_DURATION are in a class called constants.

3 Answers3

0

if you talking about for activity restore data. then see here. https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras

jihoon kim
  • 1,270
  • 1
  • 16
  • 22
0

Upon selection you can store selected values in Shared Preferences. And upon launching it again you can check if those values are null or not. And display accordingly. Read how to use Shared Preferences here.

Nilesh Deokar
  • 2,975
  • 30
  • 53
  • It does save the values but once the app goes into the background they don't save, and I have no idea why, – Christopher Mckenzie Aug 14 '17 at 15:05
  • Correct me if wrong, how user would change his colour preferences once app goes in background. since it won't be visible to user. Then what is it that you want to save when app is in background? – Nilesh Deokar Aug 14 '17 at 15:07
  • After writing out what the problem was, it's not that they're not being saved it's the fact that the selections ARE being saved but not being displayed properly. I.E by a border or the dropdown menu results back to it's default value instead of the users selection. – Christopher Mckenzie Aug 14 '17 at 15:11
  • Would you please post your activity code of saving data and using it back. – Nilesh Deokar Aug 14 '17 at 15:15
  • I've added it there to the main post, the code that saves and changes – Christopher Mckenzie Aug 14 '17 at 15:17
  • Where exactly you have extracted user selected colours from shared preferences ? – Nilesh Deokar Aug 14 '17 at 15:20
  • Added the class to the main post too – Christopher Mckenzie Aug 14 '17 at 15:26
  • Both methods are correct `saveLockScreenDuration` and `saveLockScreenDuration` but it is important to see how you are accessing these two from activity class and respective logic written in activity. So please post your activity logic where you have stored and accessed these values – Nilesh Deokar Aug 14 '17 at 15:33
  • `PreferencesStore.saveLockScreenDuration(this, index); OptionsActivity.setTimeout(this, PreferencesStore.getLockScreenDuration(this));` I think I know what you mean. Is this what you're looking for? – Christopher Mckenzie Aug 14 '17 at 15:36
  • Is it written in `onCreate()` or in `onRestart()` – Nilesh Deokar Aug 14 '17 at 15:38
  • It's in it's own method called onTimeDurationChanged – Christopher Mckenzie Aug 14 '17 at 15:46
  • I think I know where you're getting at now, thanks for the help. I'll play around with this – Christopher Mckenzie Aug 14 '17 at 15:49
  • All you need to do is set your default configuration in `onCreate` then user would select his preferences accordingly. - Now assume app goes in background. -Now app comes to foreground. In `onRestart` check if user has updated his selection by getting values using `PreferencesStore.getLockScreenDuration(this)` and then update your UI according to this value. – Nilesh Deokar Aug 14 '17 at 15:57
0

Correct me if i am wrong. I think you can use PreferenceManager.getDefaultSharedPreferences(Context context)

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

which is shared across all your Activity and Service classes. If you set an accessibility service to get the event when app is in background and used defaultsharedprefrences to store i think it will work for your case.

  • Thanks, but I've made a mistake in what I was saying it's all saving perfectly fine, but the border around the selected values aren't if that make sense. – Christopher Mckenzie Aug 14 '17 at 15:41