setSingleChoiceItems value doesn't stick after Activity kill

天涯浪子 提交于 2020-01-06 06:43:52

问题


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?


回答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!




回答2:


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!



来源:https://stackoverflow.com/questions/47982188/setsinglechoiceitems-value-doesnt-stick-after-activity-kill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!