[英]Shared Preference not saving exact instance state of android App
我正在使用Java创建一个简单的Click Counter Android应用。 我是Java新手。 我试图保存退出应用程序时的计数,无论是按下后退按钮还是关闭或崩溃,等等。这是到目前为止的代码:
public class wazeefa extends Activity {
//Count Button
TextView txtCount;
Button btnCount;
int count;
Button wmute;
Button wreset;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wazeefa);
//SAVE COUNT
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(this);
count = app_preferences.getInt("count", 0);
txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", ++count);
editor.commit();
//Button SOUND AND COUNT
final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.bubble);
txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.wclick);
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
final ImageView image = (ImageView) findViewById(R.id.imageview);
count++;
if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage);
if (count > 0) image.setImageResource(R.drawable.duroodimage);
if (count > 9) image.setImageResource(R.drawable.zikrimage);
if (count > 39) image.setImageResource(R.drawable.duroodimage);
txtCount.setText(String.valueOf(count));
mpButtonClick.start();
//RESET Button
wreset = (Button)findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
count = 0;
image.setImageResource(R.drawable.duroodimage);;
txtCount.setText("0");
}
我的应用程式有2个问题。
首先,计数没有保存到关闭应用程序然后再次打开时的状态。 例如,如果计数为“ 20”,而我按下“后退”按钮,则返回同一页面,计数将显示为“ 3”。 -有趣的是,每当我尝试上述操作并在单击“后退”按钮后又返回到应用程序时,计数都增加了1。
第二个问题是,当我返回应用程序并且计数显示为“ 5”时,例如,“重置”按钮不再起作用-它什么也不做。 但是,当我继续计数然后单击“重置”按钮时,它将计数再次更改为零。
请谁能协助解决以上两个问题?
建议后的新规范:
public class wazeefa extends Activity {
//Count Button
TextView txtCount;
Button btnCount;
Button wmute;
Button wreset;
public static int count=0;
SharedPreferences app_preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wazeefa);
//SAVE COUNT
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(this);
count = app_preferences.getInt("count", 0);
txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");}
protected void onPause() {
super.onPause();
// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
//SOUND and COUNT
final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.bubble);
txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.wclick);
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
final ImageView image = (ImageView) findViewById(R.id.imageview);
count++;
if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage);
if (count > 0) image.setImageResource(R.drawable.duroodimage);
if (count > 9) image.setImageResource(R.drawable.zikrimage);
if (count > 39) image.setImageResource(R.drawable.duroodimage);
txtCount.setText(String.valueOf(count));
mpButtonClick.start();
//RESET Button
wreset = (Button)findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
count = 0;
image.setImageResource(R.drawable.duroodimage);;
txtCount.setText("0");
}
我的代码的最后一部分:
protected void onPause() {
super.onPause();
// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
};
});
}});
};}
在班级声明为静态:
public static int count=0;
SharedPreferences app_preferences ;
并使用onPause
将SharedPreferences
计数值保存为:
protected void onPause() {
super.onPause();
// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
}
如果您希望将其保存在出口处,请将此代码发布到onDestroy()中,而不是onCreate()中
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", ++count);
editor.commit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.