0

Basically I am making a tally counter app which uses int++ to increase the number. I have managed to save whatever number the user has counted up to using SharedPreferences. Once the app is closed and releaunched the number is still there but once the user presses a button to add or subtract from the counter it restarts from 0 again. How can i make the integer counter continue from where it left off last (and example would be if the user counted up to 20 and relaunched the app they could continue counting from 20 instead of restarting from zero). Here is the code to provide some context:

public class MainActivity extends Activity implements OnClickListener{

public static final String PREFS = "examplePrefs";

Button b1;
Button b2;
TextView tv1;
TextView tv2;
int counter;
int counter2;
String stringCounter;
String stringCounter2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b1 = (Button) findViewById(R.id.button1);
    b2 = (Button) findViewById(R.id.button2);
    tv1 = (TextView) findViewById(R.id.textView1);

    b1.setOnClickListener(this);
    b2.setOnClickListener(this);

    SharedPreferences example = getSharedPreferences(PREFS, counter);
    String userString = example.getString("userMessage", "Nothing Found");
    tv1.setText(userString);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v==b1){

        counter++;
        stringCounter = Integer.toString(counter);
        tv1.setText(stringCounter);

        SharedPreferences examplePrefs = getSharedPreferences(PREFS, counter);
        Editor editor = examplePrefs.edit();
        editor.putString("userMessage", stringCounter);
        editor.commit();

    }

    if (v==b2){
        counter--;
        stringCounter = Integer.toString(counter);
        tv1.setText(stringCounter);

        SharedPreferences examplePrefs = getSharedPreferences(PREFS, counter);
        Editor editor = examplePrefs.edit();
        editor.putString("userMessage", stringCounter);
        editor.commit();
    }
}
}
user2993966
  • 3
  • 2
  • 4
  • You have taken the counter as variable in your class which you are increasing which is wrong. The value you have read in oncreate just store it in conter variable as : counter = Interger.parseInt(userString); – Sandeep Nov 19 '13 at 10:31

3 Answers3

0

You didnt assign the value of the counter when you restarted your application. so It is started from zero you should add this.

SharedPreferences example = getSharedPreferences(PREFS, counter);
String userString = example.getString("userMessage", "Nothing Found");
tv1.setText(userString);
counter = Integer.ParseInt(userString); //this line.

the added line will give the counter the value of the last session. P.S: you may need to wrap it with try and catch

-----------Edit------------

The better way to save shared Preferences is to do it in onPause() so you do it once whenever you leave the Activity. better than call it every time the button has been clicked

so it should like the following:

public class MainActivity extends Activity implements OnClickListener{

public static final String PREFS = "examplePrefs";

Button b1;
Button b2;
TextView tv1;
TextView tv2;
int counter;
int counter2;
String stringCounter;
String stringCounter2;
SharedPreferences example; // made shared Preferences global so you declare it once and make it accessible everywhere.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
tv1 = (TextView) findViewById(R.id.textView1);

b1.setOnClickListener(this);
b2.setOnClickListener(this);

example = getSharedPreferences(PREFS, counter);
String userString = example.getString("userMessage", "Nothing Found");
tv1.setText(userString);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v==b1){

    counter++;
    stringCounter = Integer.toString(counter);
    tv1.setText(stringCounter);

}

if (v==b2){
    counter--;
    stringCounter = Integer.toString(counter);
    tv1.setText(stringCounter);
}
}

@Override
protected void onPause() { // onPause() will be called whenever you leave your    activity, temporary or permanently.
    // TODO Auto-generated method stub
    super.onPause();
    Editor editor = example.edit();
    editor.putString("userMessage", stringCounter);
    editor.commit();
}

}

This way you have reduced the cost of saving in the shared preferences every time you click to once only

Coderji
  • 7,655
  • 5
  • 37
  • 51
  • thanks and also is there a way to save the number to SharedPreferences in a more efficient way instead of every time the number is changed? (maybe when the app is closed or something along those lines) – user2993966 Nov 19 '13 at 21:26
  • Please check the edited text, and if you helpful please tick it as right :) – Coderji Nov 20 '13 at 02:51
0

Just putting my comment in answer:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        tv1 = (TextView) findViewById(R.id.textView1);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);

        SharedPreferences example = getSharedPreferences(PREFS, counter);
        String userString = example.getString("userMessage", "Nothing Found");
        tv1.setText(userString);

    counter = Integer.ParseInt(userString); //add this

    }
Sandeep
  • 1,814
  • 21
  • 25
  • thanks and also is there a way to save the number to SharedPreferences in a more efficient way instead of every time the number is changed? (maybe when the app is closed or something along those lines) – user2993966 Nov 19 '13 at 12:28
0

You need to see the description of getSharedPreferences. The second argument is Mode. I think you'd better use 0.

Hao Xiao
  • 63
  • 1
  • 1
  • 6