我的共享偏好不会保存,如果我重新打开我的游戏,之前我使用SharedPreferences保存的数据未加载,我当前活动的设置再次恢复正常或默认
这是menu.class
这是我menu.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", 6);
editor.putInt("Hints", 6);
editor.putInt("Level", 1);
editor.commit();
f1=(Button)findViewById(R.id.f1);
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
f1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent i =new Intent(menu.this, levelone.class);
startActivity(i);
}
});
f2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent i =new Intent(menu.this, leveltwo.class);
startActivity(i);
}
});
f3=(Button)findViewById(R.id.f3);
f3lock=(ImageView)findViewById(R.id.f3lock);
}
public void onResume() {
super.onResume();
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
levelunlocked = pref.getInt("Level", 0);
if(levelunlocked == 2)
{
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
if(levelunlocked == 3)
{
f3.setVisibility(View.VISIBLE);
f3lock.setVisibility(View.GONE);
}
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", levelunlocked);
editor.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splashscreen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我在levelone.class
中使用了此代码来获取menu.class
int gamelifes, gamehints, gamelevel, index=0;
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
gamelifes = pref.getInt("Lifes", 0);
gamehints = pref.getInt("Hints", 0);
gamelevel = pref.getInt("Level", 0);
//the value from sharedpreferences is use to be a text by use code below
lifes1 =(TextView)findViewById(R.id.lifestext1);
lifes1.setTextColor(Color.RED);
lifes1.setText(String.valueOf(gamelifes));
hints1 =(TextView)findViewById(R.id.hintstext1);
hints1.setTextColor(Color.GRAY);
hints1.setText(String.valueOf(gamehints));
并使用新数据保存共享偏好
String answer=edittextanswer1.getText().toString();
if(answer.equalsIgnoreCase(answer1[index]))
{
gamelevel++;
image.setVisibility(View.GONE);
finishbutton.setVisibility(View.VISIBLE);
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", gamelifes);
editor.putInt("Hints", gamehints);
editor.putInt("Level", gamelevel);
editor.commit();
else
{
tryagain1.setVisibility(View.VISIBLE);
gamelifes--;
lifes1.setText(String.valueOf(gamelifes));
}
然后,如果单击完成按钮,它将是这样的
finishbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
finish();
}
});
所以levelone.class
已完成并返回menu.class
和SharedPreferences代码正常工作,my menu.class
中的每个按钮都可以使用代码并且可见!
但如果我退出应用程序,它又恢复正常
任何人都有解决方案来解决我的问题吗?
答案 0 :(得分:4)
您的onCreate
方法中有以下几行:
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", 6);
editor.putInt("Hints", 6);
editor.putInt("Level", 1);
editor.commit();
每次动作生命周期需要时都会运行onCreate
方法。特别是在打开应用程序时。因此,每次创建活动时,都会将首选项级别设置为1。这就是你问题的原因。
答案 1 :(得分:2)
事情是,在onCreate()
方法中,您始终重置共享首选项:
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", 6);
editor.putInt("Hints", 6);
editor.putInt("Level", 1);
editor.commit();
只需检查此文件是否已存在于具有if
语句的该行之前。这应该可以解决问题。
类似的东西:
if (pref.contains("Level")) {
// Do not reset
}
else {
// Create the prefs
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", 6);
editor.putInt("Hints", 6);
editor.putInt("Level", 1);
editor.commit();
}
您不必检查确切的字段,这只是一个示例。根据您的需求和游戏逻辑进行调整。
查看Activity
生命周期,了解发生这种情况的原因。
每当您打开应用时,都会调用onCreate()
方法,因此您的首选项会在您阅读之前重置。
有关生命周期的更多信息here。
编辑:以下snnipet显示了解决问题的一种方法。
public static final String PREF_LIFES = "Lifes";
public static final String PREF_HINTS = "Hints";
public static final String PREF_LEVEL = "Level";
public static final String PREF_IS_SET = "isSet";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
// Check wether the pref is set or not.
if (pref.getBoolean(PREF_IS_SET.false)) {
// The prefs is set. Do some game logic here. Like checking for lifes.
if (pref.getInt(PREF_LIFES,0) == 0) {
// Player is "dead", reset it.
resetPrefs(pref);
}
else {
// Player is alive, do whatever you want, or do nothing at all.
}
}
else {
// if it's not set, just create it.
resetPrefs(pref);
}
// ...
}
private void resetPrefs(SharedPreferences pref) {
SharedPreferences.Editor editor = pref.edit();
editor.putInt(PREF_LIFES, 6);
editor.putInt(PREF_HINTS, 6);
editor.putInt(PREF_LEVEL, 1);
editor.putBoolean(PREF_IS_SET,true);
editor.commit();
}
编辑2 :您也可以将此逻辑放在onResume()
方法上,只要您将其从onCreate()
中移除,否则您将检查这些逻辑条件两次;)
答案 2 :(得分:1)
我认为您没有在Menu.class中检查共享首选项的存在值。您只需分配一个默认值,而无需正确检查共享首选项是否包含数据。 在onCreate
在Menu.class的OnCreate上尝试这个
SharedPreferences.Editor editor = pref.edit();
//check it with your default value.I used default value as 0
int lifes=pref.getInt("Lifes", 0);
if(lifes==0)
editor.putInt("Lifes", 6);
//check it with your default value.I used default value as 0
int hints=pref.getInt("Hints", 0);
if(hints==0
editor.putInt("Hints", 6);
//check it with your default value.I used default value as 0
int level=pref.getInt("Level", 0);
if(level==0)
editor.putInt("Level", 1);
editor.commit();
答案 3 :(得分:0)
发生这种情况是因为当您启动应用程序时,menu.class会重置或通过menu.class文件onCreate()中的代码提交共享首选项数据,如下所示:
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", 6);
editor.putInt("Hints", 6);
editor.putInt("Level", 1);
editor.commit();
您需要首先检查共享首选项名称是否已提供共享首选项,如果没有,则初始化共享首选项代码如下:
if() // Check your shared preferences already not available then initialise it
{
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", 6);
editor.putInt("Hints", 6);
editor.putInt("Level", 1);
editor.commit();
}
答案 4 :(得分:0)
每次创建活动时,您都要重置已保存的Preferences
:
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", 6);
editor.putInt("Hints", 6);
editor.putInt("Level", 1);
editor.commit();
您应该只在更改时Preferences
保存。