活动杀死后setSingleChoiceItems值不粘 [英] setSingleChoiceItems value doesn't stick after Activity kill

查看:210
本文介绍了活动杀死后setSingleChoiceItems值不粘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,祝大家新年快乐!

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.

这是我的代码

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();
                }
            }
        };

问题是,在我选择了一种预设后,选择一直坚持到应用程序停止执行多任务处理(MainActivity重新启动或终止).然后,如果我重新打开该应用程序,则对话框的选择将重新设置为0(已禁用").

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?

推荐答案

是的,每次为相应的对象重新创建该字段,由于该对象(即活动)被破坏,因此保存该字段的内存被释放为出色地.因此,该字段的寿命受对象的寿命限制.为了使其连续,您最好将值保存在SharedPreferences中,或者通常在销毁活动之前将其写出到某些存储中,例如在onPause()中,然后从onCreate()onResume()回调中的那些首选项中获取它.例如:

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是共享首选项文件的文件名. KEY_NAME是键,您可以在该键下保存并随后检索所存储的值. VALUE只是要保存的值.

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.

希望这会有所帮助!

这篇关于活动杀死后setSingleChoiceItems值不粘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆