Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SharedReferences未保存_Java_Android_Sharedpreferences - Fatal编程技术网

Java SharedReferences未保存

Java SharedReferences未保存,java,android,sharedpreferences,Java,Android,Sharedpreferences,我一直在尝试保存一个名为“first”的布尔值,它表示自安装应用程序以来该活动是否已首次启动。布尔值“first”最初为true,但在活动使用一次后设置为false(即,在下一个活动开始之前,该值设置为false)。我曾尝试使用SharedReferences保存此布尔值,但每当我在杀死它后启动应用程序时,MainActivity仍会再次显示(如果“first”为false,则不应出现这种情况) 我的MainActivity.java如下所示- protected final static St

我一直在尝试保存一个名为“first”的布尔值,它表示自安装应用程序以来该活动是否已首次启动。布尔值“first”最初为true,但在活动使用一次后设置为false(即,在下一个活动开始之前,该值设置为false)。我曾尝试使用SharedReferences保存此布尔值,但每当我在杀死它后启动应用程序时,MainActivity仍会再次显示(如果“first”为false,则不应出现这种情况)

我的MainActivity.java如下所示-

protected final static String INTENT_KEY = "NAME";
private static final String PREFS_NAME = "SaveStates"; //The SharedPreferences file name.
private Boolean first = true; // Signifies whether the app started for the first time.
SharedPreferences settings;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    settings = getSharedPreferences(PREFS_NAME, 0);
    editor = settings.edit();
    resetParam(); // Retrieving the boolean "first".
    // Start the next activity if the app has been started before.
    if (!first) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        startActivity(intent);
    }
    setContentView(R.layout.activity_main);
}

/** Sends the name input by the user to the next activity */
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_name);
    String name = editText.getText().toString();
    // Send name to next activity if it's not empty.
    if (!("".equals(name))) {
        setParam(); // SETTING AND SAVING "first" AS FALSE.
        intent.putExtra(INTENT_KEY, name);
        startActivity(intent);
    }
}

/** Saving the Boolean "first" in the SharedPreferences PREF_NAME file */
private void setParam() {
    // Saving the Boolean "first" in the SharedPreferences PREF_NAME file.
    editor.clear();
    editor.putBoolean("first", false);
    editor.commit();
}

/** Retrieving the Boolean "first" from the SharedPreferences PREF_NAME file */
private void resetParam() {
    first =  settings.getBoolean("first", true);
}
当我第一次使用应用程序时(即“first”为true),转到下一个活动(即“first”在下一个活动开始前设置为false),完全关闭应用程序并返回,为什么我要再次从Main活动开始?为什么“first”未在我的SharedReferences文件(PREFS\u NAME)中保存为false你做错了

请仔细看看

一、 就个人而言,使用:

使用此选项将值保存在
SharedReferences
中:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","Harneet");
editor.apply();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("Name", "");
if(!name.equalsIgnoreCase(""))
{
    name = name + "  Sethi";  /* Edit the value here*/
}
使用此读取
SharedReferences
中的值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","Harneet");
editor.apply();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("Name", "");
if(!name.equalsIgnoreCase(""))
{
    name = name + "  Sethi";  /* Edit the value here*/
}