Note that there are some explanatory texts on larger screens.

plurals
  1. POShared Preference not saving exact instance state of android App
    primarykey
    data
    text
    <p>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:</p> <pre><code>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 &gt; 50) count = 0; image.setImageResource(R.drawable.duroodimage); if (count &gt; 0) image.setImageResource(R.drawable.duroodimage); if (count &gt; 9) image.setImageResource(R.drawable.zikrimage); if (count &gt; 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"); } </code></pre> <p>I'm having 2 issues with the app. </p> <p>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.</p> <p>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.</p> <p>Please can anyone assist in fixing the above 2 issues?</p> <p>New Code after suggestions:</p> <pre><code>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 &gt; 50) count = 0; image.setImageResource(R.drawable.duroodimage); if (count &gt; 0) image.setImageResource(R.drawable.duroodimage); if (count &gt; 9) image.setImageResource(R.drawable.zikrimage); if (count &gt; 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"); } </code></pre> <p>Final part of my code:</p> <pre><code> protected void onPause() { super.onPause(); // save count value here SharedPreferences.Editor editor = app_preferences.edit(); editor.putInt("count", count); editor.commit(); }; }); }}); };} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload