gpt4 book ai didi

java - 如果重新打开应用程序,SharedPreferences 不会保存

转载 作者:太空宇宙 更新时间:2023-11-03 12:16:47 25 4
gpt4 key购买 nike

如果我重新打开我的游戏,我的 sharedpreferences 不会保存我之前使用 SharedPreferences 保存的数据不会加载,我当前 Activity 的设置再次恢复正常或默认

这是我在 menu.class 中按钮的图像

enter image description here

这是我的 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

enter image description here

SharedPreferences 代码正常工作,my menu.class 中的每个按钮都与代码一起工作并且可见!

但如果我退出应用程序,它会再次恢复正常

enter image description here

有人有办法解决我的问题吗?

最佳答案

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 方法在每次 actvity 生命周期需要时运行。尤其是打开应用程序时。因此,每次创建 Activity 时,您都将首选项级别设置为 1。这就是您遇到问题的原因。

关于java - 如果重新打开应用程序,SharedPreferences 不会保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32050420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com