我正在编写一个Android应用程序,我在使用SharedPreferences保存int变量时遇到了困难。我已经检查了我的代码一千次,但我还没有找到问题部分。我有两个活动。在Activity1中,我有一个通向第二个按钮的按钮。在Activity2中,我有一个按钮,添加50"点"到我的变量。我将此变量保存在onPause和onStop方法中。之后我回到Activity1,我再次获得0分。
活动1:
private static final String HINT = "Hint";
private static final String VALUE = "VALUE";
int hints;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainketto); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.activity_mainketto);
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
hints = sphint.getInt(VALUE, 0);
android.support.v7.app.ActionBar actionbar = getSupportActionBar();
actionbar.hide();
Toast.makeText(MainActivity.this, "" + hints,
Toast.LENGTH_LONG).show();
Button points = (Button)findViewById(R.id.button3);
points.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,PointsActivity.class);
startActivity(i);
}
});
}
@Override
protected void onPause() {
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
SharedPreferences.Editor et2 = sphint.edit();
et2.putInt(VALUE, hints);
et2.commit();
super.onPause();
}
@Override
protected void onStop() {
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
SharedPreferences.Editor et2 = sphint.edit();
et2.putInt(VALUE, hints);
et2.commit();
super.onStop();
}
活性2:
private static final String HINT = "Hint";
private static final String VALUE = "VALUE";
int hints;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pontpiac);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.activity_pontpiac);
android.support.v7.app.ActionBar actionbar = getSupportActionBar();
actionbar.hide();
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
hints = sphint.getInt(VALUE, 0);
Toast.makeText(Pontpiac.this, ""+hints,
Toast.LENGTH_LONG).show();
Button btn = (Button)findViewById(R.id.tesztfos);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hints = hints + 50;
Toast.makeText(Pontpiac.this, ""+hints,
Toast.LENGTH_LONG).show();
}
});
@Override
protected void onPause() {
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
SharedPreferences.Editor et2 = sphint.edit();
et2.putInt(VALUE, hints);
et2.commit();
super.onPause();
}
@Override
protected void onStop() {
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
SharedPreferences.Editor et2 = sphint.edit();
et2.putInt(VALUE, hints);
et2.commit();
super.onStop();
}
如果有人知道如何让它发挥作用,请回复!
答案 0 :(得分:1)
您需要在第一个活动的onResume
中再次获得偏好
答案 1 :(得分:1)
Activity.onCreate()
通常只在首次创建Activity
时调用一次。它就像一个虚拟构造函数。当您从Activity2导航回Activity1时,不会再次调用Activity1的onCreate()
。正如@tyczj在他的回答中所说,你需要覆盖onResume()
并再次获得该值。例如:
@Override
protected void onResume() {
SharedPreferences sphint = getApplicationContext().getSharedPreferences(HINT, MODE_PRIVATE);
hints = sphint.getInt(VALUE, 0);
}
顺便说一句,我不确定你为什么要使用getApplicationContext().getSharedPreferences(...)
。您的Activity
是Context
。您只需致电getSharedPreferences(...)
即可。
答案 2 :(得分:1)
您可以使用我的库:https://github.com/mkiisoft/KeySaver是一个“超级”SharedPreference,它允许您像这样保存int,String和boolean:
KeySaver.saveShare(this, "your-key", 1); //or any int
获取值:
KeySaver.getIntSavedShare(this, "your-key");