0

Hello guys and Happy new year to all!

I'm having a weird trouble in my app which I can't seem to fix. It should be a logic error, but I'm not able to somehow catch it.

Here is my code

public String[] str={"Disabled","Sound Quality Prefered","Bass Prefered","Battery Prefered",};
public int ThemePresetValue = 0;
private int SelectedThemePresetValue = 0;

    public void presets() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Select Your Sound Preset");
    alertDialog.setNegativeButton("Cancel", null);
    alertDialog.setPositiveButton("Select", themePresetDialogPositiveListener);
    alertDialog.setSingleChoiceItems(str, ThemePresetValue, PresetListListener);
    alertDialog.show();}

DialogInterface.OnClickListener PresetListListener =
        new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                SelectedThemePresetValue = which;
            }
        };

DialogInterface.OnClickListener themePresetDialogPositiveListener =
        new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                mPreset = "";
                ThemePresetValue = SelectedThemePresetValue;

                if (ThemePresetValue == 0) {
                    mPreset = "Disabled";
                } else if (ThemePresetValue == 1) {
                    mPreset = "Sound Quality Prefered";
                } else if (ThemePresetValue == 2) {
                    mPreset = "Bass Prefered";
                } else if (ThemePresetValue == 3) {
                    mPreset = "Battery Prefered";
                }

                if (mPreset.equals("Disabled")) {
                    disabler();

                } else if (mPreset.equals("Sound Quality Prefered")) {
                    SoundQPreset();

                } else if (mPreset.equals("Bass Prefered")) {
                    bassPreset();

                } else if (mPreset.equals("Battery Prefered")) {
                    batteryPreset();
                }
            }
        };

The problem is that after I choose one of the presets the choice sticks until the app is closed from multitasking (MainActivity gets restarted or killed). Then if I re-open the app, the choice of dialog is re-set onto 0 ("Disabled").

Why is this happening? Do you have a solution?

Dimitar
  • 4,402
  • 4
  • 31
  • 47
androidexpert35
  • 319
  • 1
  • 10

2 Answers2

1

Yes, the field is created each time anew for the respective object and since this object (i.e. the activity) is destroyed the memory holding the field is freed up as well. So the field's lifespan is bounded by that of the object. To make it continuous, you better save the value in SharedPreferences, or in general to write it out to some storage, before destroying the activity, e.g. in onPause() and then fetch it from those preferences in onCreate() or onResume() callbacks. For example:

/*--- Saving ---*/

SharedPreferences prefs = 
    getApplicationContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
prefs.edit().putInt(KEY_NAME, VALUE).apply();


/*--- Retrieving ---*/

int oldValue = 
    getApplicationContext().getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE)
        .getInt(KEY_NAME, 0);

PREFERENCES_NAME is the file name of your shared preferences file. KEY_NAME is the key, under which you save and later retrieve the stored value. VALUE is simply the value to save.

Hope this helps!

Dimitar
  • 4,402
  • 4
  • 31
  • 47
1

As you are not persisting the user's choice, the choice remains in memory until activity finishes. You should save the user's choice locally using SharedPreferences or sqlite for instance!

When the activity restarts you can read the saved value and set the option as selected!

Eduardo Cucharro
  • 525
  • 1
  • 4
  • 14