如果应用程序是重新打开的共享preferences不保存 [英] SharedPreferences not Save if the application is re-open
问题描述
我的共享preferences不保存,如果我重新打开我的游戏,我与共享preferences以前保存的数据不被加载,就我目前的活动设置回来再正常或默认
这是我的按钮在 menu.class
这是下面的code我的 menu.class
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
的setContentView(R.layout.menu);
共享preferences preF = getShared preferences(SavedGame,MODE_PRIVATE);
共享preferences.Editor编辑器= pref.edit();
editor.putInt(静物,6);
editor.putInt(提示,6);
editor.putInt(级别,1);
editor.commit(); F1 =(按钮)findViewById(R.id.f1); F2 =(按钮)findViewById(R.id.f2);
f2lock =(ImageView的)findViewById(R.id.f2lock);
f1.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
意图I =新意图(菜单。该,levelone.class);
startActivity(ⅰ);
}
}); f2.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
意图I =新意图(菜单。该,leveltwo.class);
startActivity(ⅰ);
}
}); F3 =(按钮)findViewById(R.id.f3);
f3lock =(ImageView的)findViewById(R.id.f3lock);}
公共无效onResume(){
super.onResume(); 共享preferences preF = getShared preferences(SavedGame,MODE_PRIVATE);
levelunlocked = pref.getInt(级别,0); 如果(levelunlocked == 2) {
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
如果(levelunlocked == 3) {
f3.setVisibility(View.VISIBLE);
f3lock.setVisibility(View.GONE);
} 共享preferences.Editor编辑器= pref.edit();
editor.putInt(级别,levelunlocked);
editor.commit();
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.splashscreen,菜单);
返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。操作栏会
//自动处理上点击主页/向上按钮,只要
//你在AndroidManifest.xml中指定一个父活动。
INT ID = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
和我在 levelone.class
这code摆脱 menu.class
的默认值
INT gamelifes,gamehints,gamelevel,指数= 0;
共享preferences preF = getShared preferences(SavedGame,MODE_PRIVATE);
gamelifes = pref.getInt(静物,0);
gamehints = pref.getInt(提示,0);
gamelevel = pref.getInt(级别,0);//从共享preferences值被用来被下面使用code文本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));
和保存新的数据共享preferences
字符串答案= edittextanswer1.getText()的toString()。
如果(answer.equalsIgnoreCase(ANSWER1 [指数]))
{
gamelevel ++;
image.setVisibility(View.GONE);
finishbutton.setVisibility(View.VISIBLE);
共享preferences preF = getShared preferences(SavedGame,MODE_PRIVATE);
共享preferences.Editor编辑器= pref.edit();
editor.putInt(静物,gamelifes);
editor.putInt(提示,gamehints);
editor.putInt(级别,gamelevel);
editor.commit();
其他
{
tryagain1.setVisibility(View.VISIBLE);
gamelifes--;
lifes1.setText(将String.valueOf(gamelifes));
}
然后如果单击Finish按钮就会像这样
finishbutton.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(视图v){
完();
}
});
所以 levelone.class
是完成和回 menu.class
和共享preferences code其工作正常,在我menu.class
与code工作,可见<!每个按钮/ p>
但如果我退出应用程序,将其带回再次正常
任何人有一个解决方案在这里解决我的问题?
事情是,在你的的onCreate()
方法,你的总是这里重置共享preferences:
共享preferences preF = getShared preferences(SavedGame,MODE_PRIVATE);
共享preferences.Editor编辑器= pref.edit();
editor.putInt(静物,6);
editor.putInt(提示,6);
editor.putInt(级别,1);
editor.commit();
只是检查这个文件与这条线如果
语句之前已经存在。这应该做的伎俩。
是这样的:
如果(pref.contains(等级)){
//不要重置
}
其他{
//创建preFS
共享preferences.Editor编辑器= pref.edit();
editor.putInt(静物,6);
editor.putInt(提示,6);
editor.putInt(级别,1);
editor.commit();
}
您不必检查,准确的现场,这只是一个例子。调整它您的需求和游戏逻辑。
看看在活动
生命周期明白为什么会这样。
每当你打开你的应用程序中,的onCreate()
方法被调用,所以你的preFS让你阅读之前复位。
更多关于生命周期这里。
编辑:以下snnipet显示解决你的问题的一种方法。
。 公共静态最后弦乐preF_LIFES =静物
公共静态最后弦乐preF_HINTS =提示;
公共静态最后弦乐preF_LEVEL =级别;
公共静态最后弦乐preF_IS_SET =使用isset;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
的setContentView(R.layout.menu); 共享preferences preF = getShared preferences(SavedGame,MODE_PRIVATE); //请检查是否preF设置与否。
如果(pref.getBoolean(preF_IS_SET.false)){
//将preFS设置。在这里做一些游戏的逻辑。比如检查静物。
如果(pref.getInt(preF_LIFES,0)== 0){
//播放器是死的,将其复位。
重置preFS(preF);
}
其他{
//播放器是活的,做你想做什么,或者什么也不做。
}
}
其他{
//如果它没有设置,只需创建它。
重置preFS(preF);
} // ...
}私人无效复位preFS(共享preferences preF){
共享preferences.Editor编辑器= pref.edit();
editor.putInt(preF_LIFES,6);
editor.putInt(preF_HINTS,6);
editor.putInt(preF_LEVEL,1);
editor.putBoolean(preF_IS_SET,真正的);
editor.commit();
}
编辑2::你可以把你的 onResume()
方法这个逻辑,以及,只要您从<$删除C $ C>的onCreate(),否则你会检查这些条件的两倍;)
My sharedpreferences does not save if i re-open my game the data that i saved before with SharedPreferences are not load, setting on my current activity is back to normal again or default
this is the image of my button in menu.class
this is the following code of my 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);
}
}
and i had this code in levelone.class
to get the default value from 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));
and to save the sharedpreferences with new data
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));
}
then if finish button is clicked it will be like this
finishbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
finish();
}
});
so levelone.class
are finish and back to menu.class
and SharedPreferences code its worked properly, every button in my menu.class
work with the code and be visible!
but if i exit the application, its back to normal again
anyone have a solution to fix my problem here?
Thing is, on your onCreate()
method you're always resetting the shared preferences here:
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();
Just check if this file exists already before that line with an if
statement. That should do the trick.
Something like:
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();
}
You don't have to check for that exact field, it's just an example. Adjust it for your needs and game logic.
Take a look at the Activity
lifecycle to understand why this is happening.
Everytime you open your app, the onCreate()
method is called, so your prefs get reset before you read them.
More on the lifecycle here.
EDIT: The following snnipet shows one way to solve your problem.
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();
}
EDIT 2: You can put this logic on your onResume()
method as well, as long as you remove it from the onCreate()
, otherwise you'll be checking those conditions twice ;)
这篇关于如果应用程序是重新打开的共享preferences不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!