简体   繁体   English

共享首选项未保存android App的确切实例状态

[英]Shared Preference not saving exact instance state of android App

I am creating a simple Click Counter Android App using Java. 我正在使用Java创建一个简单的Click Counter Android应用。 I am new to Java. 我是Java新手。 I'm Trying to save the number of the count upon exit of the application, whether if the back button is pressed or the app is closed or crashes, etc. Here is my code so far: 我试图保存退出应用程序时的计数,无论是按下后退按钮还是关闭或崩溃,等等。这是到目前为止的代码:

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");


    }

I'm having 2 issues with the app. 我的应用程式有2个问题。

Firstly the count is not being saved to what it is when the application is closed and then opened again. 首先,计数没有保存到关闭应用程序然后再次打开时的状态。 For example if the count is at '20' and I hit the back button, then go back to the same page the count will display as '3'. 例如,如果计数为“ 20”,而我按下“后退”按钮,则返回同一页面,计数将显示为“ 3”。 - The funny thing is, that every time I have tried the above and gone back into the application after hitting the back button the count has gone up by 1 count. -有趣的是,每当我尝试上述操作并在单击“后退”按钮后又返回到应用程序时,计数都增加了1。

The second issue is that when I go back into the app and the count shows as '5' just for example, the Reset button no longer functions - it does nothing. 第二个问题是,当我返回应用程序并且计数显示为“ 5”时,例如,“重置”按钮不再起作用-它什么也不做。 But when I continue the count and then hit the Reset button it changes the count to zero again. 但是,当我继续计数然后单击“重置”按钮时,它将计数再次更改为零。

Please can anyone assist in fixing the above 2 issues? 请谁能协助解决以上两个问题?

New Code after suggestions: 建议后的新规范:

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");


    }

Final part of my code: 我的代码的最后一部分:

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

    // save count value here

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


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

Declare count as static at class level : 在班级声明为静态:

public static int count=0;
SharedPreferences app_preferences ;

and use onPause for saving count value in SharedPreferences as : 并使用onPauseSharedPreferences计数值保存为:

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

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

If you want it to be saved at exit, post this code in onDestroy() instead of onCreate() 如果您希望将其保存在出口处,请将此代码发布到onDestroy()中,而不是onCreate()中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM