共享首选项不保存Android应用程序的确切实例状态

时间:2012-12-24 12:33:49

标签: java android

我正在使用Java创建一个简单的Click Counter Android App。我是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();


         };
     });
      }});
  };}

2 个答案:

答案 0 :(得分:6)

在类级声明count为static:

public static int count=0;
SharedPreferences app_preferences ;

并使用onPauseSharedPreferences中的计数值保存为:

 protected void onPause() {
     super.onPause();

    // save count value here
   SharedPreferences.Editor editor = app_preferences.edit();
   editor.putInt("count", count);
   editor.commit();
 }

答案 1 :(得分:1)

如果您希望在退出时保存它,请将此代码发布在onDestroy()而不是onCreate()

SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", ++count);
editor.commit();