Android 中的共享首选项在我切换 activity 或关闭应用程序时未保存
Shared preferences in Android doesn't save when i switch activity or close app
我检查了类似的问题并指出似乎有效。我无法弄清楚似乎是什么问题。每次应用重新启动或 activity 切换后,值变为 0。
//just parts of code from activity1
SharedPreferences pref;
SharedPreferences.Editor editor;
// On create....
pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = pref.edit();
max=pref.getInt("mxtime", 0);
//If something>something...
editor.putInt("mxtime", max);
editor.commit();
在第一部分中,我在主 Activity 中声明了 SharedPreferences。我将它保存在 "max" int 中,它在启动时始终为 0,因为如果空值为 0。在第二个 activity 我有一个按钮,点击它应该清空 SharedPreferences 中的值。
Activity 2:
public class settings extends AppCompatActivity {
private Button myButton;
private Button myButton2;
private Button myButton3;
//sharedPrefs
SharedPreferences pref;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = pref.edit();
myButton = (Button) findViewById(R.id.button3);
myButton2 = (Button) findViewById(R.id.button4);
myButton3 = (Button) findViewById(R.id.button5);
myButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
});
myButton2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//sharedPrefs
editor.remove("mxtime");
editor.commit();
}
});
}
}
尝试像这样使用 SharedPreferences:
SharedPreferences preferences = getApplicationContext().getSharedPreferences("loginPref", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Activity 1:
SharedPreferences sp = getSharedPreferences("YourSharedPreference", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int DEFAULT_VALUE = 0;
editor.putInt("VARIABLE_KEY", DEFAULT_VALUE);
//If something > something..
int VALUE_TO_PASS = <your value here>;
editor.putInt("VARIABLE_KEY", VALUE_TO_PASS);
// Before screen shift
editor.commit();
........................................
Activity 2:
SharedPreferences sp = getSharedPreferences("YourSharedPreference", Activity.MODE_PRIVATE);
int DEFAULT_FALLBACK_VALUE = 0; //When value is not received, show this
int VALUE_PASSED = sp.getInt("VARIABLE_KEY", DEFAULT_FALLBACK_VALUE);
// On button click:
int DEFAULT_VALUE = 0;
editor.putInt("VARIABLE_KEY", DEFAULT_VALUE);
editor.commit();
我检查了类似的问题并指出似乎有效。我无法弄清楚似乎是什么问题。每次应用重新启动或 activity 切换后,值变为 0。
//just parts of code from activity1
SharedPreferences pref;
SharedPreferences.Editor editor;
// On create....
pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = pref.edit();
max=pref.getInt("mxtime", 0);
//If something>something...
editor.putInt("mxtime", max);
editor.commit();
在第一部分中,我在主 Activity 中声明了 SharedPreferences。我将它保存在 "max" int 中,它在启动时始终为 0,因为如果空值为 0。在第二个 activity 我有一个按钮,点击它应该清空 SharedPreferences 中的值。
Activity 2:
public class settings extends AppCompatActivity {
private Button myButton;
private Button myButton2;
private Button myButton3;
//sharedPrefs
SharedPreferences pref;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = pref.edit();
myButton = (Button) findViewById(R.id.button3);
myButton2 = (Button) findViewById(R.id.button4);
myButton3 = (Button) findViewById(R.id.button5);
myButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
});
myButton2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
//sharedPrefs
editor.remove("mxtime");
editor.commit();
}
});
}
}
尝试像这样使用 SharedPreferences:
SharedPreferences preferences = getApplicationContext().getSharedPreferences("loginPref", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Activity 1:
SharedPreferences sp = getSharedPreferences("YourSharedPreference", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int DEFAULT_VALUE = 0;
editor.putInt("VARIABLE_KEY", DEFAULT_VALUE);
//If something > something..
int VALUE_TO_PASS = <your value here>;
editor.putInt("VARIABLE_KEY", VALUE_TO_PASS);
// Before screen shift
editor.commit();
........................................
Activity 2:
SharedPreferences sp = getSharedPreferences("YourSharedPreference", Activity.MODE_PRIVATE);
int DEFAULT_FALLBACK_VALUE = 0; //When value is not received, show this
int VALUE_PASSED = sp.getInt("VARIABLE_KEY", DEFAULT_FALLBACK_VALUE);
// On button click:
int DEFAULT_VALUE = 0;
editor.putInt("VARIABLE_KEY", DEFAULT_VALUE);
editor.commit();