简体   繁体   中英

Shared Preference not saving exact instance state of android App

I am creating a simple Click Counter Android App using Java. I am new to 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.

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'. - 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.

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. 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 :

 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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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